質問

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top