JavaScriptExecutor in Selenium WebDriver





JavaScriptExecutor in Selenium WebDriver:
In general, we do click on an element using click() method in Selenium.

Sometimes web controls don’t react well against selenium commands and we may face issues with the above statement (click()). To overcome such kind of situation, we use JavaScriptExecutor interface.

Scrolling Web Pages using Selenium WebDriver


There are 3 different ways to Scroll Web Pages.

1.Scroll page by proving pixel number.
2. Scroll page till we find a web element on the page
3. Scroll page till bottom of the page


To scroll web page using Selenium, we can use JavaScriptExecutor interface that helps to execute JavaScript methods through Selenium Webdriver.


Syntax:
window.scrollBy(xnum, ynum)
Parameters:
  • xnum is a Number
  • Required. How many pixels to scroll by, along the x-axis (horizontal). Positive values will scroll to the right, while negative values will scroll to the left
  • ynum is a Number
  • Required. How many pixels to scroll by, along the y-axis (vertical). Positive values will scroll down, while negative values scroll up

Return Value: No return value



Examples:
Scroll page by proving pixel number
  js.executeScript("window.scrollBy(0,500)");

Scroll page till we find a web element on the page
  js.executeScript("arguments[0].scrollIntoView();",Element );

 Scroll page till bottom of the page
  js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

Test Script

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 Scrolling {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("https://www.countries-ofthe-world.com/flags-of-the-world.html");
driver.manage().window().maximize(); // maximum browser window

JavascriptExecutor js = (JavascriptExecutor) driver;

// 1. scrolling by using pixel
js.executeScript("window.scrollBy(0,1000)", "");

// 2. scrolling page till we find element
WebElement flag = driver
.findElement(By.xpath("//*[@id='content']/div[2]/div[2]/table[1]/tbody/tr[86]/td[1]/img"));
js.executeScript("arguments[0].scrollIntoView();", flag);

// 3. scroll page till bottom
js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
}
}

How To Run WebDriver Test Case using Microsoft Edge Browser


Download Microsoft WebDriver from here to launch Edge Browser. Download the proper version of the driver based on your OS build number. If the extension of the downloaded file is “.msi“, you need to install it to get the “.exe” driver.

How to Write Excel Files Using Apache POI In Selenium WebDriver


Let’s see how to Read excel files using Apache POI in Selenium WebDriver:

How To Resize Browser Window Using Selenium WebDriver



To resize browser window to particular dimensions, we use ‘Dimension’ class to resize the browser window.

How to Read Excel Files Using Apache POI In Selenium WebDriver


Let’s see how to Read excel files using Apache POI in Selenium WebDriver:

Handling Excel Files Using Apache POI In Selenium WebDriver


Selenium supports only Web browser automation. We need to get the help of third party API like Apache POI to handle (read and write) excel files using Selenium WebDriver.

How To Find Broken Links Using Selenium WebDriver





One of the key test case is to find broken links on a webpage. Due to existence of broken links, your website reputation gets damaged and there will be a negative impact on your business. It’s mandatory to find and fix all the broken links before release. If a link is not working, we face a message as 404 Page Not Found.

Double Click Action In Selenium WebDriver


In some scenarios, we may need to do double click action on a particular element to move further. In such cases, we use Actions class in Selenium WebDriver to work on Mouse and Keyboard Actions. Check out the below code for detailed explanation of Actions Class.

Right Click Action (Context Click) In Selenium


In some scenarios, we may need to do right click action / context click on an element to do some actions. We use Actions class in Selenium WebDriver to work on Mouse and Keyboard Actions. 

Drag And Drop In Selenium WebDriver


In some applications, we may face a situation to automate drag and drop an item from one location to another location.  Selenium has provided an “Actions” class to handle this kind of scenarios. 

Mouse Hover Actions Using Actions Class In Selenium


Sometimes, sub menu items render in DOM only when we mouse hover on main menu. In that case, we face difficulty to click on sub menu item. In order to perform mouse hover actions, we need to chain all of the actions that we want to achieve in one go. To do this we need to make the driver move to the parent element that has child elements and click on the child element.

How To Handle Javascript Alerts/PopUps In Selenium WebDriver


In this post, we see how to handle javascript alerts/popus. Alerts are basically popup boxes that take your focus away from the current browser and forces you to read the alert message. You need to do some action such as accept or dismiss the alert box to resume your task on the browser.

How To Upload Files Using AutoIT in Selenium


Selenium can not handle file downloading because browsers use native dialogs for downloading files. Sometime we need to download file from AUT(Application Under Test). There are several ways to automate download file in Selenium but here we see download file using AutoIT in Selenium WebDriver.




AutoIt Introduction:

AutoIt Tool is an open source tool. It is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required!
Now the question is how we do download file using AutoIT Tool in Selenium WebDriver.
Steps to integrate autoit with selenium webdriver

1) Write AutoIT script for file uploading( AutoIT Editor)


ControlFocus() --> focus on the text box

ControlSetText() --> providing path of a file
ControlClick() --> clicking on open button

2) Compile AutoIT script and generate .exe file


Tools-->Compile-->Select x64--> Compile --> generated .exe file


3) use and integrate .exe file in selenium webdriver script


Ex: 


Runtime.getRuntime().exec("C://autoitfiles/fileupload.exe"+" "+"C:\\SeleniumPractice\\Fruites\\apple.jpg");
Uploading Single File

AutoIT script

ControlFocus("File Upload","","Edit1")
Sleep(3000)
ControlSetText("File Upload","","Edit1","C:\SeleniumPractice\Fruites\apple.jpg")
Sleep(3000)
ControlClick("File Upload","","Button1")
Sleep(3000)

Note: We need to compile AutoIT script and generate .exe file (SingleFileUpload.exe)


Selenium Test case


import java.io.IOException;

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;
import org.openqa.selenium.firefox.FirefoxDriver;

public class UploadSingleFile {


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


System.setProperty("webdriver.gecko.driver","C://Drivers/geckodriver-v0.19.1-win64/geckodriver.exe");
WebDriver driver=new FirefoxDriver();

driver.get("http://demo.automationtesting.in/Register.html");

WebElement button=driver.findElement(By.xpath("//*[@id='imagesrc']"));

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", button);

Runtime.getRuntime().exec("C://SeleniumPractice/SingleFileUpload.exe"); // execute .exe file

//driver.quit();
}

}


Uploading Multiple Files

AutoIT script

Sleep(500)
ControlFocus("File Upload","","Edit1")
Sleep(500)
ControlSetText("File Upload","","Edit1",$CmdLine[1])
Sleep(500)
ControlClick("File Upload","","Button1")
Sleep(500)

Selenium Test case

import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class UploadMultipleFiles {

public static void main(String[] args) throws IOException, InterruptedException {
System.setProperty("webdriver.gecko.driver","C://Drivers/geckodriver-v0.19.1-win64/geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://demo.automationtesting.in/Register.html");
WebElement button=driver.findElement(By.xpath("//*[@id='imagesrc']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
Thread.sleep(3000);
//round1- first file
executor.executeScript("arguments[0].click();", button);
Runtime.getRuntime().exec("C://SeleniumPractice/MultipleFilesUpload.exe"+" "+ "C:\\SeleniumPractice\\Fruites\\apple.jpg");
Thread.sleep(5000);
//round2-  second file
executor.executeScript("arguments[0].click();", button);
Runtime.getRuntime().exec("C://SeleniumPractice/MultipleFilesUpload.exe"+" "+ "C:\\SeleniumPractice\\Fruites\\Mangoes.jpg");
Thread.sleep(5000);
//round3-  third file file
executor.executeScript("arguments[0].click();", button);
Runtime.getRuntime().exec("C://SeleniumPractice/MultipleFilesUpload.exe"+" "+ "C:\\SeleniumPractice\\Fruites\\PineApple.jpg");
Thread.sleep(5000);
driver.quit();
}

}

TestNG Listeners & Extent Reports in Selenium


In this post, we see TestNG listeners. Listeners “listen” to the event defined in the selenium script and behave accordingly. The main purpose of using listeners is to create logs. There are many types of listeners such as WebDriver Listeners and TestNG Listeners.

Here in this post, we see TestNG Listeners. Using TestNG listeners we could generate logs and customize TestNG Reports.

How to use HashMap in Selenium WebDriver


In this article, I discussed how to create hash map in Java for data driven tests and read the data from the HasMap object. Please go through below example.




1) HashMap in Java

Example:

public class HashMapExample {

public static void main(String[] args) {

HashMap hm = new HashMap();

// Adding pairs to HashMap
hm.put(101, "John");
hm.put(102, "Scott");
hm.put(103, "David");

// Printing pairs from HashMap
System.out.println(hm);

// Remove a pair from HashMap by using key
hm.remove(102);

System.out.println(hm);

// Retrive a single pair from HashMap using key value
System.out.println(hm.get(103));

// Reading Keys and values from HashMap
for (Map.Entry m : hm.entrySet()) {

System.out.println((m.getKey() + "  " + m.getValue()));
}

}

}

Output:

{101=John, 102=Scott, 103=David}
{101=John, 103=David}
David
101  John
103  David



2) How to use HashMap in Selenium WebDriver Test case






Example:

import java.util.HashMap;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoginTestHM {

// method return HashMap object with data pairs
static HashMap logindata() {
HashMap hm = new HashMap();
hm.put("x", "mercury@mercury");
hm.put("y", "mercury1@mercury1");
hm.put("z", "mercury2@mercury2");

return hm;
}

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

System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("http://newtours.demoaut.com/");

// Login as X
String credentials = logindata().get("x"); // Retriving value 'x' from
// HashMap

// Login as y
// String credentials = logindata().get("y");

// Login as z
// String credentials = logindata().get("x");

String uarr[] = credentials.split("@"); // separting value of 'x' int o
// 2 parts using delimeter '@'

driver.findElement(By.name("userName")).sendKeys(uarr[0]); // Passing
// value 1
// i.e
// username
// from
// array
driver.findElement(By.name("password")).sendKeys(uarr[1]); // Passing
// value 2
// i.e
// password
// from
// array
driver.findElement(By.name("login")).click();

// Validation
if (driver.getTitle().equals("Find a Flight: Mercury Tours:")) {
System.out.println("Test Passed");

} else {
System.out.println("Test failed");

}

driver.findElement(By.linkText("Home")).click();
}

}

What Is Big Data?


Big Data refers to large volume of data, that cannot be processed using traditional databases. When we have reasonable amounts of data we typically use traditional relational databases like Oracle, MySQL, SQL Server to store and work with the data. However when we have large volume of data then traditional databases will not be able to handle the data.

Followers