What Is Javascript Executor In Selenium WebDriver

In this article, will discuss about Javascript Executor in Selenium WebDriver.
 
JavaScriptExecutor is an interface provided by Selenium WebDriver. This interface allows us to execute the Javascript in the web application from Selenium WebDriver.
 
Just like for handling dropdowns, Selenium web driver has provided a class; i.e Select. Using this select class one can perform the operations on dropdowns present in web applications.
 
In a similar way, the Selenium WebDriver provides an interface to perform any Javascript execution in the web application. i.e:JavaScriptExecutor 
 

Why do we need to use JavascriptExecutor?

 
You might be thinking, "If Selenium WebDriver is able to perform all kinds of operations (sendkeys, click, etc..), then why do we need to perform operations using Javascript?"
 
The answer is: In some web applications on some controls, Selenium WebDriver operations will not be performed due to many reasons. So in those scenarios we take the help of Javascript to perform the operations.
 
JavaScriptExecutor Interface provides two methods,
  1. executeAsyncScript
  2. executeScript
executeAsyncScript
 
This method is for executing the asynchronous piece of Javascript code.
 
executeScript
 
This method is for executing the Javascript code in the browser, which includes performing actions and getting the data from the browser.
 
Let's see how it is used in test scripts,
 
As this is an interface, we can't create an instance for this but we can create a reference for this interface by assigning the WebDriver instance to JavascriptExecutor as follows,
  1. JavascriptExecutor jse = driver;  
With the above line, we get the error saying that "Type mismatch: cannot convert from WebDriver to JavascriptExecutor".
 
This is because this WebDriver is one type of interface and JavascriptExecutor is another type of interface, but if we provide the typecasting here then it works.
 
So here we need to cast WebDriver instance to JavascriptExecutor as shown below,
  1. JavascriptExecutor jse = (JavascriptExecutor) driver;  
Now the JavascriptExecutor reference is created, so we call the executeAsyncScript/executeScript methods.
 
So here I will explain the executeScript method.
 
The executeScript method definition looks like below,
  1. executeScript(java.lang.String script, java.lang.Object... args)  
script - Javascript to perform the actions.
 
args - The arguments to the script, this can be empty based on the javascript we pass.
 
Now let's display an alert using the Javascript in the web application.
 
Javascript for displaying an alert - alert('hello');
 
Now pass this javascript to the executeScript method(here this script doesn't require any arguments to be passed).
  1. JavascriptExecutor jse = (JavascriptExecutor) driver;  
  2. jse.executeScript("alert('Hello');");  
This peice of code will display an alert in the browser which is opened by the selenium webdriver.
 
Complete code
  1. WebDriver driver = new ChromeDriver();  
  2. driver.get("https://www.google.com/");  
  3. JavascriptExecutor jse = (JavascriptExecutor) driver;  
  4. jse.executeScript("alert('Hello');");  
Like this you can peform any action just by passing the required Javascipt to executeScript method.
 
In the upcoming articles, will explain how to send the text into a textbox and how to click on a button/radiobutton/checkbox/link etc...
 
If you are interested in watching this full information explained in a video, please watch this video
 
I hope you find this article useful. Please provide your valuable feedback, questions, or comments about this article.


Similar Articles