JavaScriptExecutor in Selenium WebDriver
JavaScriptExecutor in Selenium WebDriver
It takes two input parameters such as script and args. The parameter script is a
JavaScript code that has to be executed. Within the script, we will use document
object that refers to the current document.
The document object represents a web page that is displayed in the full browser
window, frame, or layer. An object is created with each document that is loaded by
the browser.
If the JavaScript code returns a value (i.e. if the script contains a return statement),
then we need to use the return keyword. The returned value is as follows:
1. WebElement for HTML element.
2. Double for decimal.
3. Long for non-decimal number.
4. Boolean for boolean.
5. String for all other cases.
6. Null if there is no return value.
Based on the type of return value, we will need to cast the executeScript() method.
For decimal values, Double will be used. For text value, String will be used. If
JavaScript code returns an HTML element, WebElement can be used, and so on.
For example:
JavascriptExecutor js = (JavascriptExecutor)driver; // Casting.
The second parameter is the args. args is the argument to the script. It may be
empty. Arguments such as a number, a boolean, a String, WebElement, or a List of
any combination of the above can also be passed to the JavaScript code being
executed by using the executedScript() method.
Scenario to Automate:
In this scenario, we will call JavaScript code to return the title of the Google home
page. In this example, we will automate the following scenario. They are as
follows:
a. Launch the Firefox browser.
b. Open the URL https://www.google.com of Google home page.
c. Get the title of the home page.
d. Close the browser.
The Java program code to automate the above scenario is as follows:
package testing;
import java.time.Duration;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import base.BaseTest;
WebDriver webDriver;
@BeforeClass
webDriver=getWebDriver();
@Test
webDriver.manage().window().maximize();
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
JavascriptExecutor js = (JavascriptExecutor)webDriver;
// The last three lines of code can also be written in one line like this:
}
}
2. Let’s take another scenario where we will send a text in the search box and click
on the submit button by using JavaScriptExecutor. We will use the same URL.
Here, we will not use the sendKeys() method.
package testing;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import base.BaseTest;
WebDriver webDriver;
@BeforeClass
public void setup() {
webDriver=getWebDriver();
}
@Test
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// Locate the WebElement Searchbox and submit button of Google
home page by By.xpath.
WebElement search =
webDriver.findElement(By.xpath("//input[@name = 'q']"));
WebElement submit =
webDriver.findElement(By.xpath("//input[@name = 'btnK']"));
// We can write Line 1 and Line 2 code in a single line like this:
// js.executeScript("arguments[0].value = 'Selenium',
arguments[1].click()", search, submit);
System.out.println("Test successful");
}
}
What is Scrollbar?
To scroll down the web page by pixel in Selenium, we will use scrollBy() method
provided by JavaScript 1.2. This method scrolls the web page in the window by a
specified number of pixels left or right and up or down. The syntax for scrollBy()
method of JavaScript is as follows:
window.scrollBy(int x-pixels, int y-pixels);
The scrollBy() method is called by using the window object by passing the number
of pixels as parameter values to the method. The object of window is automatically
created by the browser. The window object represents the browser’s window.
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import base.BaseTest;
WebDriver webDriver;
@BeforeClass
public void setup() {
webDriver=getWebDriver();
}
@Test
public void sendTextPageTest() throws InterruptedException {
// Maximize the window.
webDriver.manage().window().maximize();
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// Create the JavaScriptExecutor interface object by type casting of
WebDriver instance.
JavascriptExecutor js = (JavascriptExecutor)webDriver;
Let’s take an example where we will scroll down web page by the visibility of an
element.
Scenario to Automate:
1. Launch the Firefox browser.
2. Open the URL “https://selenium08.blogspot.com/2020/02/vertical-scroll.html”
in the Firefox browser.
3. Now, scroll the web page until the mentioned element is visible on the current
page.
[adinserter block=”2″]
To scroll down the web page until the visibility of the mentioned element, we will
use a scrollIntoView() method of JavaScript. The scrollIntoView() method is used
to scroll the web page or browser window until the mentioned element is visible on
the screen.
The general syntax for scrollIntoView() method of JavaScript is as follows:
element.scrollIntoView(alignTo)
This method accepts a boolean value that is optional. It does not return anything. A
boolean value indicates the type of alignment. Look at the source code to test the
above scenario.
Program code 2:
package testing;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import base.BaseTest;
WebDriver webDriver;
@BeforeClass
public void setup() {
webDriver=getWebDriver();
}
@Test
public void sendTextPageTest() throws InterruptedException {
// Maximize the window.
webDriver.manage().window().maximize();
js.executeScript("arguments[0].scrollIntoView();", element); // It
will scroll down the page by visibility of element "Health".
}
}
Now run the above test script and observe the output on the web page. Web page
scrolls down until the visibility of the element or not. The output of the above
scenario is shown in the below screenshot.
How to Scroll Down at the Bottom of Webpage?
Let’s take the same URL as and we will scroll down till the bottom of the page.
The scrollTo() method of Javascript scrolls until the end of the page. This method
scrolls the web page to the specified coordinates. The syntax for the scrollTo()
method is as follows:
window.scrollTo(int x, int y)
This method accepts two integer values as parameters in pixels and returns
nothing.
For example:
Scroll the web page to position “300” horizontally and “500” vertically:
window.scrollTo(300, 500);
Look at the following source code step by step.
Program code 3:
Program code 3:
package scrollUpandDown;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;