How to Process Java Script Executor in Selenium Test Automation | Devstringx

Selenium Test Automation

How to Process Java Script Executor in Selenium Test Automation | Devstringx

QUERY METHODS:

  • QuerySelector() method in HTML DOM is used to find the first HTML Element.This method function is similar to findElement() in selenium.

 Syntax:  document.querySelector(selector)

  • QuerySelectorAll() method in HTML DOM is used to return the list of HTML Elements.It can be accessed by indexing starts at 0.This method function is similar to findElements() in selenium

Syntax: document.querySelectorAll(selector)

‘document’  is an object of Javascript Document that provides access to all HTML elements of a document.

‘selector’ represents a string containing one or more CSS selectors.

How To Get Started With Query Scripts?

With the help of JavaScript Executor Interface, we can execute query scripts directly in selenium. It provides two methods such as “executeScript” & “executeAsyncScript” to run JavaScript code. To make the availability of these methods we need to downcast JavaScript Executor Interface with  Web Driver reference.

Importing the package

import org.openqa.selenium.JavascriptExecutor;

Creating a reference

JavascriptExecutor js = (JavascriptExecutor) driver;

Creating query script

script =document.querySelector(selector)

Calling the method

js.executeScript(script);

Read Also:- Write Unit Test Case on Slim 3 to Test API

QUERY SCRIPTS:

  1. click : script =document.querySelector(selector).click();
  2. send text :script =document.querySelector(selector).value=’data’;
  3. get text : script =document.querySelector(selector).textContent;
  4. get attribute : script =document.querySelector(selector).getAttribute(“class”);
  5. get height : script =document.querySelector(selector).offsetHeight;
  6. get width : script =document.querySelector(selector).offsetWidth;
  7. get style: script =document.querySelector(selector).style;

Create one simple ’QueryScriptsTest‘automation script using Selenium that searches Amazon from google by the JavascriptExecutor method.

package tests;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.remote.RemoteWebDriver;

public class QueryScriptsTest {

public static void main(String[] args) {

System.setProperty(“webdriver.chrome.driver”, “./drivers/chromedriver.exe”);

WebDriver driver=new ChromeDriver();

driver.manage().window().maximize();

driver.get(“https://www.google.com”);

JavascriptExecutor js = (JavascriptExecutor)driver;

//send text

String sendAmazonText=”document.querySelector(\”input[title=’Search’]\”).value=\”Amazon\”;”;

js.executeScript(sendAmazonText);

String clickSearchBtn =”document.querySelectorAll(\”input[value=’Google Search’]\”)[1].click()”;

//click button

js.executeScript(clickSearchBtn);

}

}

Read Also:- What is ETL Testing: Importance, Process, and Types

Output:
The above program opens the chrome browser and it navigates to google.com.It executes query scripts ‘sendAmazonText’ and ‘clickSearchBtn’ through JavaScript Executor.

Share this post