1.

What is JavaScriptExecutor and in which cases JavaScriptExecutor will help in Selenium automation?

Answer»

In selenium mostly we click on an element using click() METHOD.

Many times web controls don’t react against selenium commands and we face issues with the Click() statement. To overcome such a situation, in selenium we use the JavaScriptExecutor interface.

JavaScriptExecutor executes Javascript using Selenium driver and provides “executescript” & “executeAsyncScript” methods, to run JavaScript in the context of the currently selected frame or window.

To execute JavaScript within the browser using the Selenium WebDriver script, writing a separate script is not required. Just use a predefined interface NAMED ‘Java Script Executor’ and need to import the JavascriptExecutor package in the script.

Package:

import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.JavascriptExecutor;

Syntax:

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(Script,Arguments);

where Script is the JavaScript to be executed.

Arguments are the arguments to the script and are optional and can be empty.

JavascriptExecutor returns either Boolean, or LONG, or String, or List, or WebElement, or null.

Let us discuss some scenarios we could handle using JavascriptExecutor :

  1. Without using the sendKeys() method, you can use this interface to type Text in Selenium WebDriver.
  2. Using JavascriptExecutor, you can click on a Button.
  3. You can use it to handle the Checkboxes.
  4. It can be used to generate the Alert Pop window.
  5. You can Refresh browser window using Javascript
  6. You can get the inner text of the entire webpage in Selenium
  7. Get the TITLE of webpage
  8. Get the domain
  9. Get the URL of a webpage
  10. Perform Scroll on an application using Selenium
  11. Click on a SubMenu which is only visible on MOUSE hover on Menu
  12. Navigate to a different page using Javascript.


Discussion

No Comment Found