i
WebDriver Introduction
Selenium WebDriver Architecture
Introduction to WebDriver API
Introduction to WebDriver – Code
Handling Dropdowns and Select class
Handling Multiple Dropdown values and Links
Handling Radio buttons and Checkboxes
Capture Screenshots and Email test results
Browser Navigation Methods
Handling tabs
Capturing screenshot, Handling tabs and pop-ups – Code
Handling tabs and Pop-ups – Code
Handling Alerts
Handling User Authentication and Input alerts
HtmlUnitDriver and Handling Captchas
Handling Web Tables
Synchronization
Handling WebTables, Synchronization issues, Firefoxprofiles – Code
Actions Class
Event Listeners, Event Firing Mouse, Coordinates – Code
Handling Mouse Hover in Selenium
JavascriptExecutor
Handling Iframes
IsElementPresent, IsEnabled, IsSelected
Working with Chrome Driver - Part 1
Working with FireFox Driver - Part 2
Working with Internet Explorer Driver - Part 3
Handling SSL Certificate
Desired Capabilities
How to Encode password in WebDriver
Handling JQuery Elements - Drag and Drop, Sliders, Resizable
Handling JQuery Elements - Drag and Drop, Sliders, Resizable – Code
Working on IE Browser using Actions
TestNG, Ant & Report Generation through XSLT
Introduction to TestNG and Annotations
TestNG Parameterization
Configuring ANT, Generating TestNG & XSLT Reports
Code for generating XSLT / Surefire Reports through MAVEN
TestNG Parameterization Excel Reading
Handling Multiple data providers
TestNG XSLT Jar, Build.xml & TestNG.xml file
Frameworks Introduction
Hybrid (DATA + KEYWORD) driven Framework
Framework Architecture
Reading Excel sheets
TestNG DataProvider
Data Provider with Hashtable
Handling Multiple Test Suites
Multiple DataProviders
Setting up Run-modes at Suite Level
Setting up Runmodes at TestCase Level
Creating a common utility for Run-modes
Hybrid Framework Code
JavascriptExecutor is an Interface that is used to run javascript through Selenium WebDriver. We see some situations in Selenium where we need to handle some actions which are unable to perform by Selenium WebDriver due to some issues. Such scenarios are like scroll down a web page, clicking on an element, URL name, Domain name, Title of the page, and so on. These types of operations sometimes work very well with the help of javascript, and javascriptExecutor helps us to execute such javascript through Selenium WebDriver.
We need to import “org.openqa.selenium.JavascriptExecutor” to use javascriptExecutor in the automation script. We can see below that javascriptExecutor is an interface, and it extends the WebDriver interface.
javascriptExecutor has two methods “executeScript” and “executeAsyncScript” to execute the javascript on the selected web page. Let us see how to run a javascript by using these methods.
Asynchronous script means that the script is asynchronously executed at a breakneck speed. This means the script does not force the user to wait for a long time to render the page. This method executes asynchronous javascript in the context of the currently selected window.
We need to explicitly write that the script is finished by invoking the provided callback. The callback must be provided as the last argument into the executed method.
Creating a “wait” using the executeAsyncScript method:
Below are the steps to perform to create a wait for the Selenium script:
1-Check the Start time.
2-Run javaScript to wait for 600 ms.
3-Subtract the Start time from the current time and check if, CurrentTime-StartTime>=6000 ms.
Sample Code:
package SeleniumTest.SeleniumTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class executeAsyncScriptWait
{
public static void main( String[] args )
{
WebDriver driver=new ChromeDriver();
JavascriptExecutor execute=(JavascriptExecutor)driver;
driver.get("http://selflearning.io/");
driver.manage().window().maximize();
driver.manage().timeouts().setScriptTimeout(15, TimeUnit.SECONDS);
long startTime=System.currentTimeMillis();
execute.executeAsyncScript("window.setTimeout(arguments[arguments.length-1], 6000);");
System.out.println("Total pased time: "+ (System.currentTimeMillis()-startTime));
}
}
We can see the console output, and it shows 6109 milliseconds, which is higher than 6000 milliseconds as expected.
This method executes the javascript in the context of the current window selected. This method uses the document to refer to the current document.
Please note that once the script has finished executing, the local variables will not be available, but the global variables will be available. Now let us check an example to use executeScript method:
Click on Login button using JavascriptExecutor:
Here we need to follow the below steps to complete this flow:
1-Enter the username.
2-Enter the password.
3-Click on the login button using JavascriptExecutor.
4-Generate an alert pop-up using JavascriptExecutor.
Sample Code:
package SeleniumTest.SeleniumTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class executeScriptClick
{
public static void main( String[] args )
{
WebDriver driver=new ChromeDriver();
JavascriptExecutor jscript=(JavascriptExecutor)driver;
driver.get("http://selflearning.io/login");
driver.manage().window().maximize();
driver.manage().timeouts().setScriptTimeout(15, TimeUnit.SECONDS);
WebElement eleLoginBtn=driver.findElement(By.xpath("//*[@id='admin_login']/div[3]/div[2]/input")); driver.findElement(By.xpath("//*[@id='admin_login']/div[1]/input")).sendKeys("admin@gmail.com"); driver.findElement(By.xpath("//*[@id='admin_login']/div[2]/input")).sendKeys("admin");
jscript.executeScript("arguments[0].click();", eleLoginBtn);
jscript.executeScript("alert('Start Your Self Learning');");
}
We can see below how pop-up gets generated after clicking on login button.
Note: Here, we have taken dummy username and password, so login is not successful.
Don't miss out!