Tutorials Hut

  • Selenium WebDriver

       Selenium Introduction
       Benefits of Selenium
       Four components of Selenium
       Difference b/w Selenium IDE, RC & WebDriver
       Selenium WebDriver Architecture
       Background when user execute selenium code
       Download and Install Java
       Download and Install Eclipse
       Download Selenium WebDriver
       Selenium WebDriver Locators
       Selenium - Launch Browser
       Selenium WebDriver Waits
       Selenium- Implicit wait
       Selenium- Explicit wait
       Selenium- Fluent wait
       Selenium- Commonly used commands
       Selenium- findElement & findElements
       Selenium- Selenium-Handling check Box
       Selenium- Handling Radio button
       Selenium- Handling drop down
       Selenium- Take Screenshot
       Selenium- Handle Web Alerts
       Selenium- Multiple Windows Handling
       Selenium- Handle iframes
       Selenium- Upload a file
       Selenium- Download a file
       Selenium- Actions Class Utilities
       Selenium- Mouse Actions
       Selenium- Keyboards Events
       Selenium- Handle mouse hover Actions
       Selenium- Drag and Drop
       Selenium- Scroll a WebPage
       Selenium- Context Click / Right Click
       Selenium- Double Click
       Selenium- Desired Capabilities
       Selenium- Assertions
       Selenium- Exceptions and Exception Handling
       Selenium- Difference b/w driver.close() & driver.quit()
       Selenium- difference b/w driver.get() & driver.navigate()
       Selenium- JavascriptExecutor
       Selenium- Read excel file using Fillo API
       Selenium- Database Testing using Selenium
       Selenium- Read & write excel file using Apache POI
       Selenium- Read and Write csv file in Selenium
       Selenium- Dynamic Web Table Handling
       Selenium- Maven Integration with Selenium
       Selenium- Set up Logging using Log4j
       Selenium-Implement Extent Report



  • JavascriptExecutor in Selenium

    This article will present you with a complete idea about the JavaScriptExecutor and how we can use it if any script fails while using selenium.

    Selenium- JavascriptExecutor in Selenium

    We will learn below topics in this article

      1. What is JavaScriptExecutor?
      2. Why use JavaScriptExecutor in Selenium?
      3. What are the  javaScriptExecutor Methods?
      4. How does JavascriptExecutor work in Selenium?

    What is JavaScriptExecutor?

    JavaScriptExecutor is an interface that provides a 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.

    We can  use it directly by importing the package ‘org.openqa.selenium.JavascriptExecutor.’

    Why use JavaScriptExecutor in Selenium?

    Selenium Webdriver can perform multiple operations on web elements like sending data, clicking buttons, etc. so  sometimes webdriver commands will not work as expected due to various reasons. In such cases , we can use JavaScriptExecutor

    We use different web locator strategies like ID, Name, CSS selector, and XPath, etc for identification of webElements and If these locators do not work as expected, then we can use JavaScriptExecutor to perform the expected operation on the WebElement.

    In a similar manner some time driver.click() doesn’t work and user is unable to click then we can click on the particular element with the help of JavascriptExecutor , below is the sample command for this

    JavascriptExecutor js = ((JavascriptExecutor) driver);
    js.executeScript("arguments[0].click();", element);

    What are the  javaScriptExecutor Methods?

    JavascriptExecutor consists of two methods 

    executeAsyncScript

    This method  execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.  Asynchronous script renders the web page quickly.

    executeScript

    This method executes JavaScript in the context of the currently selected frame or window . The script fragment provided will be executed as the body of an anonymous function.

    Within the script, use document to refer to the current document. 

    If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken:

      • For an HTML element, this method returns a WebElement
      • For a decimal, a Double is returned
      • For a non-decimal number, a Long is returned
      • For a boolean, a Boolean is returned
      • For all other cases, a String is returned.

    Syntax:

    JavascriptExecutor js = (JavascriptExecutor) driver;  
    js.executeScript(Script,Arguments);
      • Script – The JavaScript to execute
      • Arguments – It is the arguments to the script. It’s optional.
      • Returns –could be anything from Boolean, Long, String, List, WebElement, or null.

    How does JavascriptExecutor work in Selenium?

    Lets understand how can we use JavascriptExecutor using below examples

    First create a reference for the interface and assign it to the WebDriver instance by type casting it.

    JavascriptExecutor js = (JavascriptExecutor) driver;  

    Now we can call the use below syntax based on the usage

    To Click on a Button

    js.executeScript("document.getElementById('enter element id').click();");
    //or
    js.executeScript("arguments[0].click();", okButton);

    To Type Text in a Text Box 

    js.executeScript("document.getElementById(id').value='someValue';");
    js.executeScript("document.getElementById('Email').value='SeleniumTesting.com';");

    To generate Alert Pop window in Selenium Webdriver

    js.executeScript("alert('Welcome To Selenium Testing');");

    To refresh browser window using Javascript

    js.executeScript("history.go(0)");

    To get the domain name

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

    To get the Title of our webpage

    String titleText =  js.executeScript("return document.title;").toString();
    System.out.println(titleText);

    To get the URL of a webpage

    String url=  js.executeScript("return document.URL;").toString();
    System.out.println(url);

    To get the Height and Width of a web page

    js.executeScript(“return window.innerHeight;”).toString();
    js.executeScript(“return window.innerWidth;”).toString();

    To navigate to a different page using Javascript

    js.executeScript("window.location = “URL”);

    To perform Scroll on an application using Selenium

    a) To scroll the page vertically for 500px
    js.executeScript(“window.scrollBy(0,500)”);
    b) To scroll the page vertically till the end
    js.executeScript(“window.scrollBy(0,document.body.scrollHeight)”);




  • Selenium WebDriver Tutorials

       Selenium Introduction
       Benefits of Selenium
       Four components of Selenium
       Difference b/w Selenium IDE, RC & WebDriver
       Selenium WebDriver Architecture
       Background when user execute selenium code
       Download and Install Java
       Download and Install Eclipse
       Download Selenium WebDriver
       Selenium WebDriver Locators
       Selenium - Launch Browser
       Selenium WebDriver Waits
       Selenium- Implicit wait
       Selenium- Explicit wait
       Selenium- Fluent wait
       Selenium- Commonly used commands
       Selenium- findElement & findElements
       Selenium- Selenium-Handling check Box
       Selenium- Handling Radio button
       Selenium- Handling drop down
       Selenium- Take Screenshot
       Selenium- Handle Web Alerts
       Selenium- Multiple Windows Handling
       Selenium- Handle iframes
       Selenium- Upload a file
       Selenium- Download a file
       Selenium- Actions Class Utilities
       Selenium- Mouse Actions
       Selenium- Keyboards Events
       Selenium- Handle mouse hover Actions
       Selenium- Drag and Drop
       Selenium- Scroll a WebPage
       Selenium- Context Click / Right Click
       Selenium- Double Click
       Selenium- Desired Capabilities
       Selenium- Assertions
       Selenium- Exceptions and Exception Handling
       Selenium- Difference b/w driver.close() & driver.quit()
       Selenium- difference b/w driver.get() & driver.navigate()
       Selenium- JavascriptExecutor
       Selenium- Read excel file using Fillo API
       Selenium- Database Testing using Selenium
       Selenium- Read & write excel file using Apache POI
       Selenium- Read and Write csv file in Selenium
       Selenium- Dynamic Web Table Handling
       Selenium- Maven Integration with Selenium
       Selenium- Set up Logging using Log4j
       Selenium-Implement Extent Report

















  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    Scroll to Top