I'm writing a test for a webapp. At one point in the application the webpage is completed using Javascript.
For testing I'm using Visual Studio 2012 with NUnit and Selenium.
I want to check if the box with id=j_idt13:JNumber has the text value of sometext.
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string valoare = (string)js.ExecuteScript("return $('#j_idt13\\:JNumber').val();");
Assert.IsTrue(valoare.Equals("sometext"));
I keep getting this error:
"Syntax error, unrecognized expression: unsupported pseudo:JNumber".
What am I missing here?
I know you have something that works but I'd like to caution you to avoid using JavaScript to fetch the value of the element, in fact in general it should be avoided when doing your tests except when there is no other way to do what you want to do. The reason is that Selenium is supposed to behave as a typical user would. Typical users don't type JavaScript into a page or interact with it directly. This goes extra for using jQuery as your tests should not assume that jQuery exists on the page and is functioning. Selenium itself provides the ability to fetch the values of fields so I'd recommend you rewrite your code to something like:
driver.FindElement(By.Id("j_idt13:JNumber")).GetAttribute("value");
After some trial and error I found something that works:
string valoare = (string)js.ExecuteScript("return document.getElementById('j_idt13\\:JNumber').value;");
Why it works, I don't know. Basically is the same command.
And jQuery is working with other commands, just not the one I tried first.
Related
I am attempting to create Coded UI tests for my organization. I'm using Selenium IDE 2.9.1, the plugin for Firefox. I'm recording my steps using the plugin, and then exporting my test case as a "C# / NUnit / WebDriver" file. I then open this file in Visual Studio 2015.
My current issue is that I cannot get the FindElement function to select the proper field. I'm using the following to select the field, but it's not working properly:
driver.FindElement(By.Id("FIELDID")).SendKeys("TEXTTOINPUT");
When I debug, this step causes Firefox to focus on the URL bar at the top of the page and then causes a System.NullReference exception.
I have searched all over, and cannot find a solution that works for my problem. I'd appreciate any information you can provide.
Regards, JM
Can you inspect the page using FireBug? That will give you the actual name of the field you are trying to find in Selenium. My guess is that either the id isn't actually set or it is dynamically generated which would make it different than what you are using the FindElement(By.Id("FIELDID")).
We used
driver.SwitchTo().Frame(driver.FindElement(By.Id("tabRequests_frame0")));
...because the item we were selecting was in a frame.
Now, I'm thinking for creating Web Automatic Testing Tool by using Selenium WebDriver with Visual Studio (C# ASP.Net).
When I create test cases, I have to make correct 'a' link's ID so that the tool can click defined links.
However, I'd like to make it automatic process, like clicking any 'a' link on the rendered page randomly for 5 minutes, for example. That means the tool will render pages until it finds any broken link.
Is it possible??
This would be possible using the page object framework as long as your links had something in common to be able to identify them.
You could Initialise the page when you first land on it and possibly use xPath selector to identify all links and put it into a List e.g.
[FindsBy(How = How.xPath, Using = "xpathToIdentifyAllLinks"]
public IList<IWebElement> Links { get; set; }
Since you have a common way to find links all you need to do is randomly select something from the Links list and click it. Then Reinitialize the page and do the same until an exception gets thrown?
The massive downside to this is if you end up with an exception getting thrown that the link is broken it will be hard to reproduce without any custom logging in place since you wont know what your test is doing.
Is there a way to select elements inside already obtained IWebElement.
Im trying to do this via C# binding (RemoteWebDriver) but
findElem.FindElements(By.CssSelector(someStmt))
returns all elements from page (not from inside findElem)
That is exactly the behavior of IWebElement.FindElement and IWebElement.FindElements. For most locators, including By.CssSelector, this works exactly as expected. The one exception is that using By.XPath, you need to prepend a "." to scope the XPath search to the current element. The WebDriver project's integration tests include tests for exactly this functionality. Without further context, like some example HTML that demonstrates the issue, further diagnosis is impossible.
Hello I've been assigned a QA ticket to validate a javascript code snippet on a page. The best way of doing this of course is to view source on page and look for the code.
However I have to do this on 20 different locales, and I've been using Selenium RC with .NET driver handily.
Is there a functionality in Selenium to check for page source code?
You could use the getHtmlSource command, but what you're asking for isn't really as easy as it sounds. Unless you just want to look and say "Yup, the script is there!".
I am developing an automation tool where one of my requirement is to detect whether the input page contains any js error. I have tried using response but this is not wokring.
Can you please help me out as I am stuck very badly.
You can't check if there is a JavaScript error on the page.
What you can do is on "trappable" errors, you can insert text into a hidden field and read that in your c# code.
But if there is an error in code then I don't think you'll be able to check that in c#.
Actually, you could have a hidden field on the page that already has text in it. Then use JavaScript to clear out the field.
In c# code, if you can see an entry in the field, then there was an error in the JavaScript and if it's empty then the JavaScript ran ok and there was no error.
Do you mean something like described here? Log client side js errors