Selenium Scripting and Troubleshoot Scenarios - Selenium Tutorials

Over a period of time, Selenium has become the most widely used test automation tool in the world and there is a huge demand for selenium experts across the world. In order to meet the ever increasing demand for selenium experts, many Selenium tutorials have come up. The selenium technology will be taught in detail in these tutorials.

When you use Selenium, though rarely, you may come across several issues and you should be prepared to overcome these hurdles. Here in this chapter of our Selenium Tutorial, we will discuss a few such trivial but important topics that would help to troubleshoot such recurring problems.

Selenium Scripting and Troubleshoot Scenarios

JavaScript Executors

There are certain actions that become the inherent part of the test scripts when a test scenario is getting automated. These actions can be any of the following.

  • Clicking a button, hyperlink etc
  • Typing in a text box
  • Scrolling horizontally or vertically to bring an object into view
  • Or some other similar actions

Usually, Selenium commands are used to automate such actions but the most embarrassing situation is when these basic Selenium commands fail to function. Although it is uncommon, it may happen sometimes. Here we will have to depend on JavaScript Executors to troubleshoot.

JavaScript Executor is a part of org.openqa.selenium and it implements java.lang.object class. JavaScriptExecutor helps to execute JavaScript directly within the web browser. Certain mechanisms in the form of methods along with a set of parameters are provided to help to execute JavaScript directly.

Methods

executeScript ( String script, args)

This method will execute the JavaScripts in the current window, alert, frame etc.

executeAsyncScript (String script, args )

This method also executes the JavaScript in the current window, alert, frame etc.

Parameters

  • Scripts: – The scripts to be executed
  • Argument: – Parameters required by the scripts for execution (if any)

In order to use JavaScriptExecutor in the test scripts, the package should be imported and the following syntax can be used for it.

import org.openqa.selenium.JavascriptExecutor;

Sample Code

Clicking a Web Element

// Locating the web element using id

WebElement element = driver.findElement(By.id(“id of the webelement”));

// Instantiating JavascriptExecutor

JavascriptExecutor js = (JavascriptExecutor)driver;

// Clicking the web element

js.executeScript(“arguments[0].click();”, element);

Typing a Text Box

// Instantiating JavascriptExecutor

JavascriptExecutor js = (JavascriptExecutor)driver;

// Typing the test data into Textbox

js.executeScript(“document.getElementById(‘id of the element’).value=’test data’;”);

Scrolling down until the web element is in the view

WebElement element=driver.findElement(By.xpath(“//input[contains(@value,’Save’)]”));

// Instantiating the javascriptExecutor and scrolling into the view in the single test step

((JavascriptExecutor)driver).executeScript(“arguments[0].scrollIntoView(true);”,element);

You will find many other ways of writing down the code for accessing JavaScriptExecutor.

Leave a Reply

Your email address will not be published. Required fields are marked *