JavaScript Executor in automation testing
In Automation testing, if QA’s stuck in some rare scenario that can not be figured out using only selenium commands here Javascript executor comes into the picture, it is a more powerful feature of Selenium WebDriver. There are many places where we execute Javascript statements through Java Selenium WebDriver. It may so happen in some real-time projects, Selenium WebDriver is not able to perform some action on a particular web element. For example, since WebDriver simulates end-user interaction, it is natural that it will refuse to click on an element that is not visible to the end-user but is present on the web page and code snippet. There are various similar scenarios like these in the automation script.
The main advantage of executing Javascript code in Selenium tests is When you cannot do something through the Selenium WebDriver classes and methods, you have another way of doing it.
QA’s may face many scenarios where they can interact with web elements only with the help of Javascript Executor.
For example:-
1. Whenever in our automation script, click() method does not click on the button:-
The click() method of the WebElement interface was executed and it did not throw any error or exception. Still, the button was not clicked.
This was not a problem of synchronization, the button was available, and with the proper state when clicking was done.
Doing the same thing through Javascript worked well. The syntax for JavaExector is as follows-
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
String query = “document.getElementById(‘elementId’).click()”;
jsExecutor.executeScript(query);
or
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
String query = “document.querySelector(“class=’search_button’]”).click()”;
jsExecutor.executeScript(query);
2. Typing a keyword in the textbox does not work:-
The code tried typing a keyword in a textbox using the sendKeys() method and nothing happened. To overcome these kinds of scenarios one can use Javascript executor.
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
String query = “document.getElementById(‘searchbox’).value = ‘java’”;
jsExecutor.executeScript(query);
3. Selecting all checkboxes from a list selected only the visible ones:-
Whenever the user wants to check whether all checkboxes are embedded in a Listbox and found that only 5 checkboxes out of 10 were displayed at a time.
To see the other ones, we would have to scroll the Listbox. The tester would think that the code was working correctly to discover that it was only checking the visible checkboxes. The ones not-visible were not checked. Again, the JavaScript executor provides the solution.
Executing Javascript in Selenium through Javascript executor significantly streamlines the process. We are a Java development company that enables enterprises to streamline operations and drive returns, using next-gen technologies, with our development services. Our ERP software development services include developing applications for all your ERP needs from CRM, WFM, and HRM to eCommerce, accounting, and wealth management software. We use an extensive tech stack including Javascript, Node.js, angular, and more to develop end-to-end customized software for your enterprise. Get in touch with our experts to know more about JavaScript Executor in automation.
META: In automation testing, javascript executor saves the day when the webdriver fails to perform an action while simulating user interaction. Read on to know how Javascript may be the most powerful feature of Selenium.