This article will discuss 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, the 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 (send-keys, 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.
- executeAsyncScript
- 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.
JavascriptExecutor jse = (JavascriptExecutor) driver;
With the above line, we get the error saying that "Type mismatch: cannot convert from WebDriver to JavascriptExecutor".
This is because 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 the WebDriver instance to JavascriptExecutor as shown below.
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 this below.
executeScript(java.lang.String script, java.lang.Object... args)
- script: Javascript to perform the actions.
- args: The arguments to the script, 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).
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("alert('Hello');");
This piece of code will display an alert in the browser which is opened by the selenium webdriver.
Complete code
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("alert('Hello');");
Like this, you can perform any action just by passing the required Javascript to executeScript method.
The upcoming articles will explain how to send the text into a textbox and how to click on a button/radio button/checkbox/link, etc...
I hope you find this article useful. Please provide your valuable feedback, questions, or comments about this article.