Selenium Webdriver Tutorial Javascript

Selenium WebDriver tutorial Step by Step. Selenium(Java) Advance. Reading External Data. Read Write Excel using Apache POI. Once we switch the control from browser to the alert window. We can use the Alert Interface methods to do required actions such as accepting the alert, dismissing the alert, get the text from the alert window, writing some text on the alert window etc.

What is JavaScriptExecutor?

JavaScriptExecutor is an Interface that helps to execute JavaScript through Selenium Webdriver. JavaScriptExecutor provides two methods 'executescript' & 'executeAsyncScript' to run javascript on the selected window or current page.

In this tutorial, you will learn -

Why do we need JavaScriptExecutor?

In Selenium Webdriver, locators like XPath, CSS, etc. are used to identify and perform operations on a web page.

In case, these locators do not work you can use JavaScriptExecutor. You can use JavaScriptExecutor to perform an desired operation on a web element.

Selenium supports javaScriptExecutor. There is no need for an extra plugin or add-on. You just need to import (org.openqa.selenium.JavascriptExecutor) in the script as to use JavaScriptExecutor.

JavaScriptExecutor Methods

  1. executeAsyncScript
Selenium

With Asynchronous script, your page renders more quickly. Instead of forcing users to wait for a script to download before the page renders. This function will execute an asynchronous piece of JavaScript in the context of the currently selected frame or window in Selenium. The JS so executed is single-threaded with a various callback function which runs synchronously.

  1. executeScript

This method executes JavaScript in the context of the currently selected frame or window in Selenium. The script used in this method runs in the body of an anonymous function (a function without a name). We can also pass complicated arguments to it.

The script can return values. Data types returned are

  • Boolean
  • Long
  • String
  • List
  • WebElement.

The basic syntax for JavascriptExecutor is given below:

Syntax:

  • Script – This is the JavaScript that needs to execute.
  • Arguments – It is the arguments to the script. It's optional.

Example of executeAsyncScript

Using the executeAsyncScript, helps to improve the performance of your test. It allows writing test more like a normal coding.

The execSync blocks further actions being performed by the Selenium browser but execAsync does not block action. It will send a callback to the server-side Testing suite once the script is done. It means everything inside the script will be executed by the browser and not the server.

Example 1: Performing a sleep in the browser under test.

In this scenario, we will use 'Guru99' demo site to illustrate executeAsyncScript. In this example, you will

  • Launch the browser.
  • Open site 'http://demo.guru99.com/V4/ '.
  • Application waits for 5 sec to perform a further action.

Step 1) Capture the start time before waiting for 5 seconds ( 5000 milliseconds) by using executeAsyncScript() method.

Step 2) Then, use executeAsyncScript() to wait 5 seconds.

Step 3) Then, get the current time.

14 new dinosaurs. Jpog mod download. new skins for every dinosaur, building and vehicle.ini modding for every dinosaur.

Step 4) Subtract (current time – start time) = passed time.

Step 5) Verify the output it should display more than 5000 milliseconds

Output: Successfully displayed the passed time more than 5 seconds(5000 miliseconds) as shown below:

Example of executeScript

For executeScript, we will see three different example one by one.

1) Example: Click a button to login and generate Alert window using JavaScriptExecutor.

In this scenario, we will use 'Guru99' demo site to illustrate JavaScriptExecutor. In this example,

  • Launch the web browser
  • open the site 'http://demo.guru99.com/V4/ 'and
  • login with credentials
  • Display alert window on successful login.

Output: When the code is executed successfully. You will observe

  • Successful click on login button and the
  • Alert window will be displayed (see image below).

2) Example: Capture Scrape Data and Navigate to different pages using JavaScriptExecutor.

Novatel wireless mc760 driver. Execute the below selenium script. In this example,

  • Launch the site
  • Fetch the details of the site like URL of the site, title name and domain name of the site.
  • Then navigate to a different page.

Output: When above code is executed successfully, it will it will fetch the details of the site and navigate to different page as shown below.

3) Example: Scroll Downusing JavaScriptExecutor.

Execute the below selenium script. In this example,

  • Launch the site
  • Scroll down by 600 pixel

Output: When above code is executed, it will scroll down by 600 pixels (see image below).

Summary:

JavaScriptExecutor is used when Selenium Webdriver fails to click on any element due to some issue.

  • JavaScriptExecutor provides two methods 'executescript' & 'executeAsyncScript' to handle.
  • Executed the JavaScript using Selenium Webdriver.
  • Illustrated how to click on an element through JavaScriptExecutor, if selenium fails to click on element due to some issue.
  • Generated the 'Alert' window using JavaScriptExecutor.
  • Navigated to the different page using JavaScriptExecutor.
  • Scrolled down the window using JavaScriptExecutor.
  • Fetched URL, title, and domain name using JavaScriptExecutor.
Selenium Webdriver Tutorial Javascript

Training Summary

Selenium is a popular open-source web-based automation tool. This online course is a step by step guide to learn Selenium Concepts. It is recommended you refer the tutorials sequentially, one after the other.

What should I know?

This free tutorial is designed for beginners with little or no automation experience. If you are new to testing first take the basic Software Testing class.

Want Selenium Lessons in your Inbox? Join our Email Course

Syllabus

First Look
TutorialWhat is Selenium? Introduction to Selenium Automation Testing
TutorialWhat is Selenium WebDriver? Difference with RC
WebDriver Tutorial

It will be beneficial if you revisit Java, before reading tutorials on Webdriver

TutorialHow to Download & Install Selenium WebDriver
TutorialFirst Selenium Webdriver Script: JAVA Code Example
TutorialLocators in Selenium IDE: CSS Selector, DOM, XPath, Link Text, ID
TutorialFind Element and FindElements in Selenium WebDriver
TutorialSelenium Form WebElement: TextBox, Submit Button, sendkeys(), click()
TutorialHow to Select CheckBox and Radio Button in Selenium WebDriver
TutorialHow to Click on Image in Selenium Webdriver
TutorialHow to Select Value from DropDown using Selenium Webdriver
TutorialLocate Elements by Link Text & Partial Link Text in Selenium Webdriver
TutorialMouse Click & Keyboard Event: Action Class in Selenium Webdriver
TutorialHow to Upload & Download a File using Selenium Webdriver
TutorialXPath in Selenium WebDriver: Complete Tutorial
TutorialAlert & Popup Window Handling in Selenium WebDriver
TutorialHow to Handle Web Table in Selenium WebDriver
TutorialHandling Dynamic Web Tables Using Selenium WebDriver
TutorialDesired Capabilities in Selenium WebDriver
TutorialHow to Verify Tooltip using Selenium WebDriver
TutorialHow to Find All/Broken links using Selenium Webdriver
TutorialGecko (Marionette) Driver Selenium: Download, Install, Use with Firefox
TestNG
TutorialHow to Download & Install TestNG in Eclipse for Selenium WebDriver
TutorialTestNG Tutorial: Annotations, Framework, Examples in Selenium
TutorialTestNG Groups: Include, Exclude with Example - Selenium Tutorial
TutorialTestNG @Test Priority in Selenium
TutorialParallel Execution in Selenium: Session Handling & TestNG Dependency
TutorialTestNG: How to Run Multiple Test Suites in Selenium
TutorialTestNG Listeners in Selenium: ITestListener & ITestResult Example
TutorialHow to Execute Failed Test Cases in TestNG: Selenium WebDriver
TutorialTestNG Report Generation in Selenium WebDriver
TutorialCustomize, PDF & Email TestNG Reports in Selenium WebDriver

Selenium Webdriver Example

Frameworks
TutorialPage Object Model (POM) & Page Factory: Selenium WebDriver Tutorial
TutorialDataprovider & TestNG XML: Parameterization in Selenium(Example)
TutorialRead & Write Data from Excel File in Selenium Webdriver: POI & JXL
TutorialHow to Select Date from DatePicker/Calendar in Selenium Webdriver
Advance Webdriver Stuff!
TutorialSelenium Grid Tutorial: Hub & Node (with Example)
TutorialMaven & Jenkins Integration with Selenium: Complete Tutorial
TutorialSelenium Automation Framework: Data Driven, Keyword Driven & Hybrid
TutorialDatabase Testing using Selenium: Step by Step Guide
TutorialHandling iFrames in Selenium Webdriver: switchTo()
TutorialCross Browser Testing using Selenium WebDriver
TutorialHow to Take Screenshot in Selenium WebDriver
TutorialLog4j with Selenium Tutorial: Download, Install, Use & Example
TutorialSelenium Headless Browser Testing: HTMLUnitDriver & PhantomJS
TutorialRobot Class in Selenium Webdriver
TutorialHow to use AutoIT with Selenium Webdriver: File Upload Example
TutorialHow to Handle SSL Certificate in Selenium WebDriver
TutorialHow to Handle AJAX Call in Selenium Webdriver
TutorialJavaScriptExecutor in Selenium WebDriver with Example
TutorialSelenium Webdriver using Python: Tutorial with Example
TutorialHow to use IntelliJ IDEA & Selenium Webdriver
TutorialFlash Testing with Selenium WebDriver
TutorialApache ANT with Selenium: Complete Tutorial
TutorialHow to Generate XSLT Report in Selenium Webdriver
TutorialGithub Integration with Selenium: Complete Tutorial
TutorialCookies Handling in Selenium WebDriver
TutorialUsing SoapUI with Selenium for Web Service Testing
TutorialHow to Create Firefox Profile in Selenium WebDriver
TutorialSelenium with Cucumber (BDD Framework): Tutorial with Example
TutorialHow to Drag and Drop in Selenium WebDriver (EXAMPLE)
TutorialSelenium C# Webdriver Tutorial: NUnit Example
TutorialCreating Object Repository in Selenium WebDriver: XML & Properties file
TutorialHow to Scroll Down or UP a Page in Selenium Webdriver
TutorialSikuli Tutorial: How to use Sikuli with Selenium (EXAMPLE)
TutorialXPath Contains, Sibling, Ancestor Functions in Selenium WebDriver
TutorialImplicit, Explicit, & Fluent Wait in Selenium WebDriver
TutorialDouble click and Right Click in Selenium with Examples
TutorialSelenium Proxy Authentication using Webdriver with EXAMPLE
Selenium IDE Tutorial

Selenium With Javascript

TutorialHow to Download & Install Selenium IDE for Firefox
TutorialSelenium IDE Tutorial for Beginners
TutorialHow to use Selenium IDE with Scripts & Commands (Assert, Verify)
TutorialVerify Element Present, waitFor, andWait in Selenium IDE
TutorialStore Variables, Echo, Alert, PopUp handling in Selenium IDE
TutorialSelenium Core Extensions (User-Extensions.js)
TutorialBreakpoint & Start Point in Selenium IDE
TutorialMaximize Browser in Selenium
TutorialChrome Options & Desiredcapabilities: AdBlocker, Incognito, Headless
TutorialRefresh Page using Selenium Webdriver
Must Check!
TutorialTop 100 Selenium Interview Questions & Answers
TutorialSelenium vs HP UFT (QTP): What's the Difference?
TutorialTop 15 Selenium Alternatives
TutorialSelenium Tutorial PDF

Selenium Webdriver Tutorial Javascript Download

Live Selenium Project

Selenium Webdriver Js

JoinLive Selenium Project: Banking Domain
JoinLive Ecommerce Project: Selenium Automation