JavascriptExecutor powers up Selenium to handle scenarios

Category: Selenium WebDriver 70 2

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:

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:

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?

How to refresh window in Selenium?

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

How to navigate to the different page in Selenium?

How to get current URL in Selenium?

How to get the domain name in Selenium?

How to click on sub-menu through Mouse hover?

How to hide element in Selenium at runtime?

How to show element in Selenium at runtime?

How to navigate back in Selenium using JavascriptExecutor?

How to navigate forward in Selenium using JavascriptExecutor?

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.

Related Articles

2 thoughts on “JavascriptExecutor powers up Selenium to handle scenarios

  1. Chandan Chakraborty

    Thanks for Sharing excellent Information about “JavascriptExecutor powers up Selenium to handle scenarios”, I love your article. I will definitely share your article on my social networks. Again thank you!
    Keep sharing this type of article!

    Reply

Add Comment