JavascriptExecutor Interface-executeScript() Method


Automation Testing: Selenium-JavascriptExecutor

JavascriptExecutor Interface has below 2 methods.
1. executeScript()
2. executeAsyncScript()

We use JavascriptExecutor when we fail to perform some actions using Selenium.


if we try to enter any text in a greyed-out box OR disabled box using sendKeys() method of WEBELEMENT interface,  we get InvalidElementStateException.

Using executeScript() of JavascriptExecutor interface, we can enter text in a disabled textbox.

Sample WebPage: DisabledTextBox.html

"<Html>
<Body>
<label for="fname">Enable TextBox:</label>
<input id="En",type="text", Value ="Enabled"></br></br>
<label for="fname">Disable TextBox:</label>
<input id="Dis",type="text", Value ="Disabled", Disabled="True">
</Body>
</Html>"



Example

public class DisabledTextbox {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("file://E:/testData/DisabledTextBox.html");
//Typecast the driver object to JavascriptExecutor interface type
JavascriptExecutor js = (JavascriptExecutor) driver;
Thread.sleep(1500);
//enter "ankit" in first textbox using javascript
js.executeScript("document.getElementById('En').value=’ankit’");
Thread.sleep(2000);
//clear the value in second textbox using javascript
js.executeScript("document.getElementById('Dis').value=''");
//enter "vijay" in second textbox using javascript
js.executeScript("document.getElementById('Dis').value='vijay'");
//change the second text box to button type using Javascript
js.executeScript("document.getElementById('Dis').type='button'");
}}

Uses of JavascriptExecutor
1. To scroll on the webpage,
2. To handle the disabled elements,
3. To use as an alternate solution when Selenium inbuilt methods [e.g. clear(), click(),
sendKeys() ] doesn’t work.

In Selenium , we don’t have any method to scroll up or down on the webpage, in such case, we can use JavascriptExecutor.

Steps to run javascript manually on browser webpage
1. Open the required page in the browser and press F12 from keyboard.
2. Navigate to Console tab, type the javascript statement and press Enter key

Write a script to scroll up and down on Selenium official website
Using executeScript() of JavascriptExecutor interface

Example 
public class ScrollUpandDown {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://SELENIUMhq.org/download");
//typecasting driver object to JavascriptExecutor interface type
JavascriptExecutor js = (JavascriptExecutor) driver;
for (int i = 1; i < 10; i++) {
//scroll down on the webpage
js.executeScript("window.scrollBy(0, 1000)");
Thread.sleep(3000);
}
for (int i = 1; i < 10; i++) {
//scroll up on the webpage
js.executeScript("window.scrollBy(0, -1000)");
Thread.sleep(3000);
}}}

Example : Write a script to scroll down to the bottom of the page?

Using executeScript() of JavascriptExecutor interface


public class NavigatetoBottomofthePage
 {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.SELENIUMhq.org/download/");
driver.findElement(By.id("close")).click();
//select an element which is present at the bottom of the page
WebElement element = driver.findElement(By.id("footerLogo"));
int x = element.getLocation().getX();
int y = element.getLocation().getY();
System.out.println("X coordinate is:"+x + " and Y coordinate is:"+ y);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy("+x+","+y+")");
Thread.sleep(3000);
element.click();
}}
I
Questionsnterview Question:

When do we go for Javascriptexecutor?

When Selenium conventional methods fails to perform an action on the webpage, we go for
javascriptexecutor.

How do you use javascriptExecutor?
We typecast the driver object to javascriptexecutor interface, once we have the reference, we
call executeScript() method, to which, as an argument, we pass the actual javascript statement.

How do you get the javascript statement from the browser?

Right click anywhere on the page, click on inspect and then navigates to console tab.

Write the lines of code:
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('Ena').value='Ankit'");

Post a Comment

1 Comments

  1. The project management was flexible and easy, as the team excelled at communication, speed, skill, and availability.
    Bay Area web designers

    ReplyDelete
Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)