4

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?

farheen
  • 1,786
  • 1
  • 15
  • 22

3 Answers3

4

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);
}
farheen
  • 1,786
  • 1
  • 15
  • 22
  • Can I disable JavaScript on Chrome using this function? Some like this `loadjsFile.doDisableJavascript(driver);` – paul Feb 13 '18 at 11:18
1

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

Bob Evans
  • 616
  • 6
  • 18
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'");
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
  • 1
    thanks for the reply. But, in my application, there is js file loaded with 100's of functions. Should i write each function through JSExecutor.What should i do, to re use those functions? – farheen Jan 19 '12 at 05:39
  • Are you performing other commands (ie: doStore) inside of those functions? If so, that won't work. You need to isolate each command to a given line in your object oriented code now with this ExecuteScript method (unless I find some documentation online proving me incorrect). I've looked everywhere myself. An alternative would be to write a class that contains all your functions and just get constants (or static methods if your do* functions/commands had parameters) for your scripts passed to ExecuteScript. – JustBeingHelpful Jan 19 '12 at 15:25