Part 4: Usages Of Javascripts In Selenium: Understanding Method executeScript Of JavascriptExecutor

Hello folks,

In this post, we will learn about method executeScript of JavascriptExecutor interface.

JavascriptExecutor interface provides two methods to execute JavaScript commands.

  1. executeScript : To execute synchronized JavaScript script commands
  2. executeAsyncScript: To execute asynchronous JavaScript commands

We will learn about “executeScript ” method in this post.

Synatx:
java.lang.Object executeScript(java.lang.String script,java.lang.Object… args)

As per selenium doc, above method executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function.

What is meant by “The script fragment provided will be executed as the body of an anonymous function.”?

It means, if we write as below:

driver.executeScript(“console.log(‘Hello.’);”);

Selenium will execute above JavaScript command (with in double quotes inside method) as below:

Arguments to method executeScript :

executeScript method takes two arguments:

  1. javaScript command which you want to run in form of String. It is mandatory.
  2. An array of arguments. It is optional. It is same as we have in main() method of java program. These arguments will be used in javaScript command.

Let’s understand this by an example:

You can see I have parameterized JavaScript command and passed arguments value as String. Now run the program and see output in console:

You can see all parameterized (arguments[0] and arguments[1]) are replaced by arguments we passed. In the same way you can pass as ,many as arguments you want. Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the “arguments” magic variable, as if the function were called via “Function.apply”.

Return type of method executeScript :

executeScript returns an Object type as it may return different types of values based on javaScript command is executed.

If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken:

  • For an HTML element, this method returns a WebElement
  • For a decimal, a Double is returned
  • For a non-decimal number, a Long is returned
  • For a boolean, a Boolean is returned
  • 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.
  • For a map, return a Map<String, Object> with values following the rules above.
  • Unless the value is null or there is no return value, in which null is returned

To get the return value from javascript command, we just need to append “return” statement in javaScript command. See the example below:

Output:

You can see output as “false” which is returned by executeScript method and we have printed it on console.

Hope you will have clear understanding of method executeScript. 

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning