JavascriptExecutor powers up Selenium to handle scenarios

Selenium and JavaScript have the long-term relationship since the Selenium evolved. The earlier version of Selenium (i.e., Selenium 1) was fully based on JavaScript and Selenese. Even in advanced Selenium which we are currently using (Selenium WebDriver) does not have a solid solution to handle some of the operations like element is not clickable, not visible, element highlighter, etc. Therefore, Selenium WebDriver implemented a separate interface (JavascriptExecutor) to handle out of the box scenarios through JavaScript.

Selenium deals on Out of the box scenarios with JavascriptExecutor

In our previous tutorials, we have discussed some of the out of the box scenarios which WebDriver implements with the help of JavascriptExecutor by using JavaScript commands. It is highly recommended that you must learn these tutorials. Here is the list for you:

I am 100% sure that these tutorials will help you somewhere in your Selenium Automation development journey.

What is JavaScript?

JavaScript is one among the group of OOP (Object Oriented Programming Language). It is basically used to perform interaction with the HTML and the elements in DOM. It signifies that all the browsers have an implementation to understand and send the response to the JavaScript commands. You can even disable the JavaScript from your browser’s settings

Advantages of JavaScript

  • It changes the content of the HTML through innerHTML
  • JavaScript changes the attributes of the HTML
  • It also changes the CSS Style
  • It can entirely change, hide and show the HTML elements
JavascriptExecutor in Selenium

Image Credit: Medium

What is JavascriptExecutor?

JavascriptExecutor is an interface in Selenium WebDriver, which is basically used to inject the JavaScript code at runtime for the purpose of functional testing. It performs the actions using two methods, which are discussed as follows:

1. executeScript() method

This method used to pass complicated arguments in the body of the random methods. It executes the command by considering the context of the window or the frames.

The return type of executeScript() method is listed as follows:

  • Long
  • List
  • WebElement
  • String
  • Boolean

Most of the time we use executeScript()  method for our scenarios. Here is the syntax for it:

JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeScript(script, arguments);

2. executeAsyncScript() method

It is more powerful than the executeScript() method. It is also best for performance improvement of your Selenium Automation test. The browser executes everything which is written inside the script, not by the server, as it sends call back to the server-side testing. Whereas, executeScript() method blocks the execution.

Here is the syntax for executeAsyncScript() method:

JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeAsyncScript(script, arguments);

Package to import for JavascriptExecutor

org.openqa.selenium.JavascriptExecutor;

This was all introductions to the importance of JavascriptExecutor in Selenium. Let’s have a look at some of the miscellaneous scenarios handled by Selenium and JavaScript altogether.

Miscellaneous JavaScriptExecutor commands in Selenium

JavascriptExecutor

How to generate Alert pop up in Selenium?

JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeScript(“alert(‘Welcome to inviul’);”);

How to refresh window in Selenium?

JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeScript(“history.go(0)”);

How to get the title of the web page in Selenium?

JavascriptExecutor js = (JavascriptExecutor)driver;

String title = Js.executeScript(“return document.title;”).toString();

How to navigate to the different page in Selenium?

JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeScript(“window.location = “http://www.inviul.com/avinash-mishra””);

How to get current URL in Selenium?

JavascriptExecutor js = (JavascriptExecutor)driver;

String url = Js.executeScript(“return document.URL;”).toString();

How to get the domain name in Selenium?

JavascriptExecutor js = (JavascriptExecutor)driver;

String domainName = Js.executeScript(“return document.domain;”).toString();

How to click on sub-menu through Mouse hover?

JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeScript(“$(‘ul.menus.menu-secondary.sf-js-enabled.sub-menu li’).hover()“);

How to hide element in Selenium at runtime?

JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeScript(“document.getElementById(‘some_id’)[0].style.display=’none’”);

How to show element in Selenium at runtime?

JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeScript(“document.getElementById(‘some_id’)[0].style.display=block”);

How to navigate back in Selenium using JavascriptExecutor?

JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeScript(“window.history.back()”);

How to navigate forward in Selenium using JavascriptExecutor?

JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeScript(“window.history.forward()”);

That’s all about Selenium and its relation to JavaScript using JavascriptExecutor. If you have any doubts and confusions then feel free to connect with me.

2 Comments

Leave a Reply