Thursday, September 19, 2019

Post#71.JavascriptExecutor in Selenium


What is JavascriptExecutor?

JavascriptExecutor is an interface of Selenium API that has the functionality similar to that of Java Script and can interact with HTML DOM elements. Instead of using driver.findElement method of the Selenium WebDriver we can use JavaScriptExecutor Interface to perform similar action on the Page.
JavaScriptExecutor provides two methods "executescript" & "executeAsyncScript" to run javascript on the selected window or current page.

In Selenium Webdriver, locators like XPath, CSS, etc. are used to identify and perform operations on a web page. In case, these locators do not work you can use JavaScriptExecutor.

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

Script – This is the JavaScript that needs to execute.
Arguments – It is the arguments to the script. It's optional.

Examples:

Type text in textbox: (SendKeys Operation)
Synatax:
js.executeScript("document.getElementById('some id').value='someValue';");
Example:
js.executeScript("document.getElementById('Email').value='google.com';");
Click on webElement: (Click Operation)
js.executeScript("document.getElementById('enter your element id').click();");
//or
js.executeScript("arguments[0].click();", loginButton);

Click on a SubMenu which is only visible on mouse hover on Menu
js.executeScript("$('ul.menus.menu-secondary.sf-js-enabled.sub-menu li').hover()");

To navigate to different page
js.executeScript("window.location = 'https://www.testingbar.com");

To handle Checkbox:
js.executeScript("document.getElementById('enter element id').checked=false;");

To Generate Alert Popup:
js.executeScript("alert('Welcome To Selenium');");  

To Refresh Browser:
js.executeScript("history.go(0)");       

To get the Title of our webpage
String sText =  js.executeScript("return document.title;").toString();
System.out.println(sText);

To get the domain
String domainName =  js.executeScript("return document.domain;").toString();
System.out.println(domainName);

To get the URL of a webpage
String url =  js.executeScript("return document.URL;").toString();
System.out.println(url);

Highlighting Element:
((JavascriptExecutor)driver).executeScript("arguments[0].style.border='3px solid red'", element);

get innertext of the entire webpage
JavascriptExecutor js = (JavascriptExecutor)driver;
String sText =  js.executeScript("returndocument.documentElement.innerText;").toString();

Stop Page buffering
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("return window.stop");


To perform Scroll on application
window.scrollBy(xnum, ynum)

xnum is a Number
How many pixels to scroll by, along the x-axis (horizontal). Positive values will scroll to the right, while negative values will scroll to the left
ynum is a Number
How many pixels to scroll by, along the y-axis (vertical). Positive values will scroll down, while negative values scroll up

Example 1: (250 pixels vertical down scrolling)
JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript("window.scrollBy(0,250)", "");

Example 2: (250 pixels vertical up scrolling)
JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript("window.scrollBy(0,-250)", "");

Similarly you can perform horizontal scrolling by changing xnum parameter.

Example 3: To scroll to the Bottom of the Web Page
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");

Example 4: Scroll to specific element
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", element);



No comments:

Post a Comment