I'm using Selenium client driver 2.5.0 with the WebBackedSeleniumDriver. I want to inject a Javascript function for use across my tests, but am having some trouble. Here's how I initialize my driver …
final FirefoxDriver driver = new FirefoxDriver();
selenium = new WebDriverBackedSelenium(driver, baseUrl);
开发者_如何学Gobut the second line below throw an "'inc' undefined error."
selenium.getEval("function inc(x) { return x + 1; }); ");
String incResult = selenium.getEval("inc(5);");
Any ideas how I can successfully inject a Javascript function for use in other tests?
Thanks, - Dave
Please try to change slightly as follows.
selenium.getEval("inc = function(x) { return x + 1; }");
String incResult = selenium.getEval("inc(5);");
According to the selenium docs (Selenium Driver .NET Docs):
Creates a new "script" tag in the body of the current test window, and adds the specified text into the body of the command. Scripts run in this way can often be debugged more easily than scripts executed using Selenium's "getEval" command. Beware that JS exceptions thrown in these script tags aren't managed by Selenium, so you should probably wrap your script in try/catch blocks if there is any chance that the script will throw an exception.
So, the only change I do on the answer is to change the getEval in favor of runScript method.
精彩评论