top of page
Writer's picturesamudramdeepthi

Mastering of JavaScriptExecutor in selenium 

Usage of JavaScriptExecutor in selenium

Demo:

public class JavaScriptExecutorDemo2 {

 

    public static void main (String [] args) throws InterruptedException {

        // Setup WebDriver

        WebDriverManager.chromedriver(). setup ();

        ChromeDriver driver = new ChromeDriver ();

 

        // Navigate to the W3Schools JavaScript alert example

 

        // Create a JavaScriptExecutor instance

        JavascriptExecutor jsexec = (JavascriptExecutor) driver;

 

        // Print the title of the page

        String script = "return document. title;";

        String title = (String) jsexec. executeScript(script);

        System.out.println(title);

 

        // Click the button and handle the alert

        driver. switchTo().frame("iframeResult");

        jsexec. executeScript("myFunction ()");

        Thread.sleep(2000);

        driver. switchTo (). alert (). accept ();


        //Highlighted Button

       WebElement button = driver.findElement(By.xpath("/html/body/button"));

       jsexec.executeScript("arguments[0].style.border='5px solid green'", button);

 

       //Navigating to homepage

       driver.navigate().to("https://www.w3schools.com/");

       WebElement certifiedbutton = driver.findElement(By.xpath("//*[@id=\"certsection\"]/p/a"));

       jsexec.executeScript("arguments[0].scrollIntoView(true);", certifiedbutton);

 

    }

}



Code for javaScriptExecutor

Demonstration of above code:

·      Setup WebDriver:

     Initializes the ChromeDriver for automated browser interactions.

·      Navigate to the Page:

     Opens the specified W3Schools page with a JavaScript alert example.

·      Create JavaScriptExecutor:

             Creates an instance of JavascriptExecutor to execute JavaScript code within the browser context.

·      Print Page Title:

      Uses executeScript to execute the JavaScript code return document. title; to get the page title and prints it to the console.


Finding Elements in console using javascript

·      Click Button and Handle Alert:

              Switches to the iframe containing the alert button.

·      Executes the JavaScript function myFunction () to trigger the alert.

·      Waits for 2 seconds to allow the alert to appear.

·      Switches to the alert and accepts it using accept ().

Document Object Locators in javascript:

 

Methods in javascript

·      Find Element:

        The first line locates a button element on the web page using its XPath. XPath is a language for navigating and selecting nodes                 in an XML document (which HTML is a variant of).

·       Highlight:

          The second line uses JavaScript to execute a script that styles the found button element with a green border. This is a common technique to visually highlight elements during testing or debugging.

·      Scroll:

·       Navigate:

·                The first line navigates the browser to the specified URL (W3Schools website).

·       Find Element:

·                The second line finds a button element on the W3Schools page using its XPath.

·       Scroll:

           The third line uses JavaScript to execute a script that scrolls the page until the found button element is visible on the screen.  This is useful when the element is located outside the current viewport.

 

Advantages:

 * JavascriptExecutor allows for dynamic interactions with web pages that may not be possible with standard WebDriver methods.

 * It's often used to execute JavaScript code that interacts with elements, triggers events, or modifies the DOM.

 * In this example, it's used to click a button that triggers a JavaScript alert and then handles the alert.

Notes:

 * Consider using a more robust waiting mechanism like WebDriverWait instead of Thread.sleep() to ensure that the alert is fully loaded before attempting to interact with it.

Purpose:

The purpose of JavaScriptExceutor in Selenium WebDriver test is they are designed to automate interactions with web elements, such as highlighting them for visibility or scrolling to them when they are not in view. This can be helpful for testing web applications, debugging issues, or performing regression testing on websites.

27 views

Recent Posts

See All
bottom of page