Why we use it?
To enhance the capabilities of the existing scripts by performing javascript injection into our application under test.
In simple words “Javascript can be executed within the browser with the help of JavaScript Executor.”
Package:-
import org.openqa.selenium.JavascriptExecutor;
Syntax:-
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);
script - The JavaScript to execute
Arguments - The arguments to the script.(Optional)
Scenario’s
1.How to generate Alert Pop window in selenium?
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
Js.executeScript("alert('hello world');");
2.How to click a button in Selenium WebDriver using JavaScript
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
3.How to refresh browser window using Javascript ?
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.executeScript("history.go(0)");
4.How to get innertext of the entire webpage in Selenium?
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
string sText = js.executeScript("return document.documentElement.innerText;").toString();
5.How to get the Title of our webpage ?
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
string sText = js.executeScript("return document.title;").toString();
6.How to perform Scroll on application using Selenium
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
//Vertical scroll - down by 50 pixels
js.executeScript("window.scrollBy(0,50)");
Note:- for scrolling till the bottom of the page we can use the code like
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
7.How to click on a SubMenu which is only visible on mouse hover on Menu?
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
//Hover on Automation Menu on the MenuBar
js.executeScript("$('ul.menus.menu-secondary.sf-js-enabled.sub-menu li').hover()");
8.Implement Highlight in Selenium?
9.How to navigateto different page using Javascript?
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
//Navigate to new Page
js.executeScript("window.location = 'https://www.facebook.com/uftHelp'");
Working Demo:-
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; public class JavaScriptExecuter { public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
//Launching the browser application
driver.get("http://www.uftHelp.com");
//Adding wait
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Maximize window
driver.manage().window().maximize();
//Creating the Javascriptexecutor interface object by Type casting
JavascriptExecutor js = (JavascriptExecutor)driver;
//Fetching the Domain Name
String sDomain = js.executeScript("return document.domain;").toString();
System.out.println("Domain = "+sDomain);
//Fetching the URL
String sURL = js.executeScript("return document.URL;").toString();
System.out.println("URL = "+sURL);
//Fetching the Title
String sTitle = js.executeScript("return document.title;").toString();
System.out.println("Title = "+sTitle);
//Vertical scroll - down by 200 pixels
js.executeScript("window.scrollBy(0,200)");
System.out.println("Successfully did the vertical scroll by 200px"); } }