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

  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Print Triangle Pattern Flowchart
  • RAPTOR Editions
  • Flowgorithm Conditional Breakpoint Statement
  • Flowgorithm Read Numbers from File Example
  • Search Text File Flowchart Example
  • Flowgorithm Turtle Graphics Symbols
  • Draw Circle using Flowgorithm Turtle

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version