Arguments – It is the arguments to the script. It’s optional.
It is useful for custom synchronizations, hide or show the web elements, change values, test flash/HTML5, and so on.
executeScript method – This method executes the test script in the context of the currently selected window or frame in Selenium. We can also pass complicated arguments to it. If the script has a return statement, the following values are returned:
· For an HTML element, the method returns a WebElement.
· For a decimal, the method returns Long.
· For a non-decimal number, the method returns Long.
· For a Boolean, the method returns Boolean.
· For other cases, the method returns a String.
· For all other cases, a String is returned.
· For an array, return a List<Object> with each object following the rules above. We support nested lists.
· Unless the value is null or there is no return value, in which null is returned.
With Asynchronous script, your page renders more quickly Instead of forcing users to wait for a script to download before the page renders.
· The JS so executed is single-threaded with a various callback function which runs synchronously.
· Using the executeAsyncScript, helps to improve the performance of your test.
· It allows writing test more like a normal coding.
· execAsync does not block action being performed by the Selenium browser.
· It will send a callback to the server-side Testing suite once the script is done.It means everything inside the script will be executed by the browser and not the server.
1.To click a button without using click() method:
2. To type Text without using sendKeys() method:
3. To select checkbox:
js.executeScript(“document.getElementById(‘enter element id’).checked=false;”);
4. To generate Alert Pop window:
5. To refresh browser window:
6. To get innertext of the entire webpage:
String sText = js.executeScript(“return document.documentElement.innerText;”).toString();
7. To get the Title of a webpage:
String sText = js.executeScript(“return document.title;”).toString();
8. To get the domain:
String sText = js.executeScript(“return document.domain;”).toString();
9. To get the URL of a webpage:
String sText = js.executeScript(“return document.URL;”).toString();
10. To perform Scroll operations:
//Vertical scroll down by 500 pixels:
//scroll down till the bottom of the webpage:
11. To click on a SubMenu which is only visible on mouse hover on Menu:
12. To navigate to different page:
js.executeScript(“window.location = ‘pageUrl’”);
13. To find hidden element:
I hope this blog was useful to the Reader. Thank you.