TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Selenium

    Selenium JavascriptExecutor Examples

    Overview

    In this post we will discuss Selenium Webdriver JavascriptExecutor Examples. JavascriptExecutor is an interface indicating that the WebDriver can execute JavaScript and providing access to the mechanism to do so

    JavascriptExecutor Examples

    JavascriptExecutor executes JavaScript in the context of the currently selected frame or window. As a result, the script fragment provided will be executed as the body of an anonymous function.

    Same origin policy : Because of cross domain policies browsers enforce your script execution may fail unexpectedly  and without adequate error messaging.

    executeScript()

    We can pass the script the JavaScript to execute by the method.This method executes JavaScript in the context of the currently selected frame or window.As a result,the script  provided will be executed as the body of an anonymous function. Furthermore, one can use document within the script, to refer to the current document.

    Example:

    JavascriptExecutor jscript = (JavascriptExecutor)driver;
    jscript.executeScript("scroll(0, 500)");

    We can even pass arguments . Furthermore, arguments must be a number, a boolean, a String, WebElement, or a List of any combination. Also, an exception will be thrown if the arguments do not meet these criteria. Also, the arguments will be made available to the JavaScript via the “arguments” magic variable.

    Example:

    if (webdriver instanceof JavascriptExecutor)
     {
     ((JavascriptExecutor)webdriver).executeScript("arguments[0].
    style.border='6px solid green'", webelement);
    }

     

    executeAsyncScript()

    Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window. Unlike executing  executeScript synchronous JavaScript, scripts executed with this method must explicitly signal they are finished by invoking the provided callback. This callback is always injected into the executed function as the last argument.

    The first argument passed to the callback function will be used as the script’s result.Also,the default timeout for a script to be executed is 0ms. Most of all, one must set the script timeout beforehand to a sufficiently large value.

    Example:

    Performing a sleep in the browser under test.

     long start = System.currentTimeMillis();
          ((JavascriptExecutor) driver).executeAsyncScript(
              "window.setTimeout(arguments[arguments.length - 1], 750);");
          System.out.println(
              "Elapsed time: " + System.currentTimeMillis() - start);
        }

    AJAX request is usually sent using XMLHttpRequest and wait for the ready state for the callback.
    We will discuss about AJAX in later post.

    —

    Selenium Tutorials on this website:

    https://www.testingdocs.com/selenium-webdriver-tutorial/

    Official Website:

    https://www.selenium.dev/

    Related Posts

    Windows 10 Settings

    Selenium /

    Add Microsoft Webdriver on Windows OS

    Download Selenium Components

    Selenium /

    Getting Started with Selenium Webdriver

    LambdaTest Testing Cloud SaaS Platform

    Selenium /

    LambdaTest – Testing Cloud SaaS Platform

    Selenium /

    Selenium 3.0 and Mozilla GeckoDriver

    SauceLabs Website

    Selenium /

    Run an Example Test on SauceLabs

    ‹ Page Object @FindBy Annotation› Selenium : Working with Cookies

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com