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

可紊 提交于 2019-12-06 06:04:38

问题


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



来源:https://stackoverflow.com/questions/8325968/how-to-call-the-user-extensions-js-in-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!