How to call the user-extensions.js in Selenium Web

2019-04-10 09:08发布

问题:

I need to use the functions defined in user-extensions.js.We are in a process of migration from RC to webdriver. I came to know, that there is JavascriptExecutor which will replace runScript and getEval.But how will i specify the user-extensions.js file. Is it same as java -jar selenium-server-standalone.jar -userExtensions user-extensions.js?

回答1:

They now have an interface called IJavaScriptExecutor that can be used to replace user extensions.

This is the C#/NUnit version.

IJavaScriptExecutor js = driver as IJavaScriptExecutor;
long tableRowCount = (long)js.ExecuteScript("return $('#tableid tr').length);

Here is the Java/JUnit version:

JavascriptExecutor js = (JavascriptExecutor) driver;
Object o = js.executeScript("return '123'");


回答2:

Finally, after 2 years and 2 months I found a solution to use user-extensions file in webdriver and now we are migrating to webdriver.

 loadjsFile(driver);

Function is below:

public static void loadjsFile(WebDriver driver){
String scriptSrc = "http://localhost:8080/test/user-extensions.js";
String injectScript = "var script = document.createElement(\"script\");";
injectScript += "script.src = \""+scriptSrc+"\";";
injectScript += "script.setAttribute(\"type\",\"text/javascript\");";
injectScript += "document.body.appendChild(script);";
((JavascriptExecutor) driver).executeScript(injectScript);
}


回答3:

There is not a way to inject javascript that is available through your test run like Selenium RC, here is a thread about some possible migration tips: http://groups.google.com/group/selenium-developers/browse_thread/thread/15cb4b774b734cc7/c7baf10db0bc2bc0