What is JavaScriptExecutor in Selenium? – User friendly Tech help
What is JavaScriptExecutor?
n
nJavaScriptExecutor is an interface which provides mechanism to execute Javascript through selenium driver. It provides “executescript” & “executeAsyncScript” methods, to run JavaScript in the context of the currently selected frame or window.
n
Lets inject Javascript into Browser using Selenium |
n
Why we use it?
n
nTo enhance the capabilities of the existing scripts by performing javascript injection into our application under test.
n
nIn simple words “Javascript can be executed within the browser with the help of JavaScript Executor.”
n
nPackage:-
n
nimport org.openqa.selenium.JavascriptExecutor;
n
nSyntax:-
n
n
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(Script,Arguments);
n
n
nscript – The JavaScript to execute
nArguments – The arguments to the script.(Optional)
n
n
nScenario’s
n
n1.How to generate Alert Pop window in selenium?
n
nCode:-
n
JavascriptExecutor js = (JavascriptExecutor)driver;
Js.executeScript("alert('hello world');");
n
n2.How to click a button in Selenium WebDriver using JavaScript
n
nCode:-
n
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
n
n3.How to refresh browser window using Javascript ?
n
nCode:-
n
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.executeScript("history.go(0)");
n
4.How to get innertext of the entire webpage in Selenium?
n
nCode:-
n
JavascriptExecutor js = (JavascriptExecutor)driver;
string sText = js.executeScript("return document.documentElement.innerText;").toString();
n
n5.How to get the Title of our webpage ?
n
Code:-
n
JavascriptExecutor js = (JavascriptExecutor)driver;
string sText = js.executeScript("return document.title;").toString();
n
6.How to perform Scroll on application using Selenium
n
nCode:-
n
JavascriptExecutor js = (JavascriptExecutor)driver;
//Vertical scroll - down by 50 pixels
js.executeScript("window.scrollBy(0,50)");
n
nNote:- for scrolling till the bottom of the page we can use the code like
n
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
n
n
7.How to click on a SubMenu which is only visible on mouse hover on Menu?
n
nCode:-
n
JavascriptExecutor js = (JavascriptExecutor)driver;
//Hover on Automation Menu on the MenuBar
js.executeScript("$('ul.menus.menu-secondary.sf-js-enabled.sub-menu li').hover()");
n
8.Implement Highlight in Selenium?
n
n9.How to navigate to different page using Javascript?
n
nCode:-
n
JavascriptExecutor js = (JavascriptExecutor)driver;
//Navigate to new Page
js.executeScript("window.location = 'https://www.facebook.com/uftHelp'");
n
n
n
nWorking Demo:-
n
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"); }}