I'm using selenium to test a webapp.
It has to do with adding Announcements/data. It has alot of inputs...
Problem: Randomly, text-A, meant for input-A get typed in input-B as well as text-B.
Since there's a lot of repetition, I read text-inputs from xml and return a dictionary. And type text as so
public AnnouncementAdvertiserFields TypeAdvertiserFields(string pathToXml)
{
var xmlParser = new XmlParser();
Dictionary<string, string> fields = xmlParser.TypeAdvertiserFieldsFromXml(pathToXml);
string name;
string coAddress;
string streetName;
string streetNo;
string streetFloor;
string streetDoor;
string city;
string postalCode;
string postalCity;
string phoneNo;
string mobileNo;
string faxNo;
string country;
string journalNo;
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.Name, out name);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.CoAdress, out coAddress);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.Streetname, out streetName);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.StreetNumber, out streetNo);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.StreetFloor, out streetFloor);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.StreetDoor, out streetDoor);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.City, out city);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.PostalCode, out postalCode);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.PostalCity, out postalCity);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.PhoneNumber, out phoneNo);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.MobilePhoneNumber, out mobileNo);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.FaxNumber, out faxNo);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.Country, out country);
fields.TryGetValue(WebTesting.Common.Constants.AdvertiserFieldNames.JounalNo, out journalNo);
if (name != string.Empty) TypeName(name);
if (coAddress != string.Empty) TypeCoAddress(coAddress);
if (streetName != string.Empty) TypeStreetName(streetName);
if (streetNo != string.Empty) TypeStreetNumber(streetNo);
if (streetFloor != string.Empty) TypeStreetFloor(streetFloor);
if (streetDoor != string.Empty) TypeStreetDoor(streetDoor);
if (city != string.Empty) TypeCity(city);
if (postalCode != string.Empty) TypePostalCode(postalCode);
if (postalCity != string.Empty) TypePostalCity(postalCity);
if (phoneNo != string.Empty) TypePhoneNumber(phoneNo);
if (mobileNo != string.Empty) TypeMobilePhoneNumber(mobileNo);
if (faxNo != string.Empty) TypefaxNumber(faxNo);
if (country != string.Empty) SelectCountryByValue(country);
if (journalNo != string.Empty) TypeJournalNumber(journalNo);
return this;
}
Example of TypeName from AnnouncementAdvertiserFields
public void TypeName(string name)
{
TypeText(name, _nameInputLocator);
}
TypeName calls generic TypeText method from a superclass.
protected void TypeText(string text, By locator)
{
Webdriver.FindElement(locator).SendKeys(text);
}
Type AnnouncementAdvertiserFields is a property on the page object page of the specific announcement type.
I have tried using both implicit wait and explicit wait. With sooooooo many combinations of ExpectedConditions
TextToBePresentInElement before and after SendKeys
ElementExists
ElementIsVisible
I would think code like this should work
var wait = new WebDriverWait(Webdriver, TimeSpan.FromSeconds(5));
wait.Until(ExpectedConditions.ElementIsVisible(locator));
var element = Webdriver.FindElement(locator);
element.Clear();
wait.Until(ExpectedConditions.TextToBePresentInElement(element, ""));
element.SendKeys(text);
Wait.Until(ExpectedConditions.TextToBePresentInElement(FindStdkElement(locator), text));
I also tried using the SelectElement but to no avail.
If I debug my way through, everything is dandy and great. No errors. But when I run the test, sometimes it passes other times fails. I cannot find any system as to which fields fails. It's random...
When I run the tests from my local machine I have no issues. But when run from machine in DEV, they fail sporadically. DEV-machine is less powerfull than local, which makes my think it might be a timing issue. That maybe Selenium is is typing to fast for the browser to keep up.
I use Nunit as testframework. With ReSharper.
Any help or directions will be much appreciated. thanks guys
I think there can be timing issues. If there is time out then increase the time out that you have given and then run it again and check weather there is failed tests in you DEV machine.
To increase timeout. Try this...
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Reference - http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
Related
I want to convert a string to enum . My code is like this
var categoryEnum = (SiteCategory) Enum.Parse(typeof (SiteCategory), UIController.QueryStringParamInitialValue, true);
And QueryStringParamInitialValue() is like this
public static string QueryStringParamInitialValue
{
get
{
string str = string.Empty;
if (HttpContext.Current != null && HttpContext.Current.Session != null && HttpContext.Current.Session["UIController_QueryStringParamInitialValue"] != null)
str = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Session["UIController_QueryStringParamInitialValue"] as string);
return str;
}
set
{
HttpContext.Current.Session["UIController_QueryStringParamInitialValue"] = (object)HttpContext.Current.Server.UrlEncode(value);
}
}
I am facing error in var categoryEnum = (SiteCategory) Enum.Parse(typeof (SiteCategory), UIController.QueryStringParamInitialValue, true);
Any information regarding this will be helpful..pls help
If seeing this error as a part of a Xamarin project:
As mentioned here I had to "launch xCode at least one time after it's update." for this error to go away. I also had to restart Visual Studio after the xCode update and launch.
After running veracode scan, I got the CWE 113 error. I had found a solution to replace the cookie value, but still the issue is not fixed.
Fix for CWE-113: Improper Neutralization of CRLF Sequences in HTTP
Headers ('HTTP Response Splitting')
string ReplaceHTTPRequestValue(string Value)
{
string replacedValue = string.Empty;
if (!string.IsNullOrEmpty(Value))
{
replacedValue = Value.Replace("\r", string.Empty)
.Replace("%0d", string.Empty)
.Replace("%0D", string.Empty)
.Replace("\n", string.Empty)
.Replace("%0a", string.Empty)
.Replace("%0A", string.Empty);
}
return replacedValue;
}
void WebTrends_PreRender()
{
HttpCookie cookie = Request.Cookies["WT_CID"];
string campaignIdVal = string.Empty;
if (cookie != null)
{
campaignIdVal = ReplaceHTTPRequestValue(Request.Cookies["WT_CID"].Value);
}
else
{
campaignIdVal = string.Empty;
}
}
How can I solve this?
Please take a look at this link
https://community.veracode.com/s/question/0D53n00007YVaMrCAL/how-to-fix-flaws-for-cwe-id-113-http-response-splitting
It is likely the reason the flaw continues to be reported is because
the functions you are using are not in the list of Supported Cleansing
Functions, which you can find in the Help Center here:
https://help.veracode.com/go/review_cleansers. For example the
supported function org.owasp.encoder.Encode.forJava() would cleanse
for CWE-113, as well as CWE-117, CWE-80 and CWE-93. Please note that
it is important to select the appropriate cleansing function for the
context.
string ReplaceHTTPRequestValue(string Value)
{
string NonCRLF = string.Empty;
foreach (char item in Value)
{
NonCRLF += item.ToString().Replace("\n", "").Replace("\r","");
}
return NonCRLF;
}
I am trying to detect if my testcase failed that it should do some process which I have mentioned it in my code below. I am successfully able to detect when my testcase passed.
I have added my code. My code here does is to navigate to google homepage if it passed then I should get a txt file with a "[MethodName] - Passed.txt" with a text in it Passed. Same for fail.
[Test]
public void Initialize()
{
PropertiesCollection.driver = new TWebDriver();
LoginPageObject objLogin = new LoginPageObject();
string pathfile = #"file location";
string sheetName = "Login";
var excelFile = new ExcelQueryFactory(pathfile);
var abc = from a in excelFile.Worksheet(sheetName).AsEnumerable()
where a["ID"] == "3"
select a;
foreach (var a in abc)
{
PropertiesCollection.driver.Navigate().GoToUrl(a["URL"]);
}
foreach (var a in abc)
{
objLogin.Login(a["uname"], a["paswd"]);
}
StackFrame stackFrame = new StackFrame();
MethodBase methodBase = stackFrame.GetMethod();
string Name = methodBase.Name;
GetFiles(Name);
}
public void GetFiles(string testcase)
{
if ((TestContext.CurrentContext.Result.Status == TestStatus.Failed) || (TestContext.CurrentContext.Result.State == TestState.Failure) || (TestContext.CurrentContext.Result.State == TestState.Ignored) || (TestContext.CurrentContext.Result.State == TestState.Error))
{
string destpath = (#"destination location");
File.WriteAllText(Path.Combine(destpath, testcase + " - Failed" + ".txt"), "Failed");
}
else
{
string destpath = (#"destination location");
File.WriteAllText(Path.Combine(destpath, testcase + " - Passed" + ".txt"), "Passed");
}
}
Here, only else condition is identified but not with if condition.
Question: Can anyone identify what part I am missing for fail test case.
Note:
Please forgive me if I have not explained properly. If you donot understand please ask question.
For each part is actually an error. If there is an error it should create a txt file as per my code in GetFiles(string testcase) which I am unable to do so.
I am using Nunit to identify whether my test case passed or fail. So if testcase passes or fails a text is created mentioning that to keep a record of my failed test case.
Appreciate your help.
If a test fails, the [Test] method is terminated, and the rest of the method never run. So, if your test were to fail mid-way through, GetFiles() would never be called.
You probably mean to run GetFiles() as a [TearDown] to achieve what you're trying to do.
As an aside, I'd recommend doing:
TestContext.CurrentContext.Result.Status != TestStatus.Success
rather than you current if - as you seem to have found already, there are many different varieties of failure!
It looks like you have a typo. Your if statement can never be true as written. You want an 'or' instead of an 'and'
if ((TestContext.CurrentContext.Result.Status == TestStatus.Failed) ||
(TestContext.CurrentContext.Result.State == TestState.Failure) ||
(TestContext.CurrentContext.Result.State == TestState.Ignored) || // <-- typo?
(TestContext.CurrentContext.Result.State == TestState.Error))
{
Folks,
I'm doing a post on a web page with emoji / emoticon. But after posted the site does not display the emoticon. Must you use a different Encoding? If so how can I do?
Example have this emoji 👐💓⛪🌇 the site only shows me that ⛪ Other special characters appear.
if (currentElement.GetAttribute("type") == "submit")
if (currentElement.Name == "view_post")
{
string postagem = txtPublicacao.Text;
HtmlElement elea = webBrowser1.Document.GetElementById("u_0_0");
if (elea != null)
elea.SetAttribute("value", postagem);
currentElement.InvokeMember("click");
}
I think you can prevent yourself from experiencing some future grief by ensuring that blocks are enclosed in brackets like so:
if (currentElement.GetAttribute("type") == "submit")
{
if (currentElement.Name == "view_post")
{
string postagem = txtPublicacao.Text;
HtmlElement elea = webBrowser1.Document.GetElementById("u_0_0");
// if condition and response either on one line:
if (elea != null) elea.SetAttribute("value", postagem);
// ...or use "{}" in preparation for possible future additions to the reponse to the if condition
if (elea != null)
{
elea.SetAttribute("value", postagem);
}
currentElement.InvokeMember("click");
}
}
Or better yet, since you have two consecutive "ifs" before code is executed, combine them like so:
if ((currentElement.GetAttribute("type") == "submit") &&
(currentElement.Name == "view_post"))
{
string postagem = txtPublicacao.Text;
HtmlElement elea = webBrowser1.Document.GetElementById("u_0_0");
if (elea != null) elea.SetAttribute("value", postagem);
currentElement.InvokeMember("click");
}
Hello
I have a method listed bellow:
public static PasswordCredential Create(string password, string username, string pinCode = null)
{
Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(password), "Invalid Argument: password");
Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(username), "Invalid Argument: username");
PasswordCredential credential = new PasswordCredential();
UTF8Encoding encoder = new UTF8Encoding();
SHA512Managed sha512hasher = new SHA512Managed();
credential.PasswordHash = sha512hasher.ComputeHash(encoder.GetBytes(password)); <-- Requires unproven: s != null
credential.Username = username;
credential.PinCode = pinCode;
return credential;
}
Does this mean that Contract.Requires doesn't prove the expression? If so what's the use for it? :?
UPDATE
Ok, I found a very weird behavior of code contracts. I moved this method to another project and it worked. No more requires unproven warnings. Then I get back to the original project, and in Messages window, I found this line:
Message 1 CodeContracts: Suggested precondition: Contract.Requires(password != null);
When I double click this item, I'm navigated to PasswordCredential's PinCode property.
[DataMember]
public string PinCode
{
get { return _PinCode; }
set { _PinCode = value == null ? null : value.Trim(); } <-- I'm navigated here
}
but when I click the warning item, I'm navigated to encoder.GetBytes(password) line.
I don't understand what's wrong. Is it a bug?
Ok
There's an issue with CC (which can be reproduced only in my project :D). I sent the project to CC team and got very fast response. They are investigating and probably it will be fixed in the next release.
Anyway thanks for your support guys.