Headless Browsers
A headless browser is a web-browser without a graphical user interface. This program will behave just like a browser but will not show any GUI.
Advantages
1) It Is not using any GUI to test so your tests will run In background without any visual Interruption.
2) Execution Is faster compared to all other driver Instances.
3) Run-time can be reduced by up to 50% for most tasks.
4) Can be executed without taking up the screen context of a computer.
Disadvantages
1) Hard to debug inconsistent failures on locating elements due to page loading too fast
2) Unintended interactions due to speed/headless state of browser
Few Headless Drivers- for JAVA
1) HtmlUnit
2) PhantomJS
HTML Unit
HtmlUnit is an open source java library for creating HTTP calls which imitate the browser functionality. HtmlUnit is mostly used for integration testing on top of unit test frameworks such as JUnit or TestNG. HtmlUnit is a great test automation tool for driving simulated browsers for testing web applications.
Downloading and setting up HtmlUnit
Download HtmlUnit jar file from below path
https://sourceforge.net/projects/htmlunit/files/
This download jar file comes as a ZIP file named "htmlunit-version.zip", extract the contents of this ZIP file on your local drive. This directory contains all the JAR files that we would later import on Eclipse.
Writing your first test in Java for HtmlUnit
1) Create a Java Project.
2) Associate below mentioned jar files
a) Latest Selenium-java client jar files
b) Latest HtmlUnit Driver jar files
packagehtmlunittest;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.htmlunit.HtmlUnitDriver;
publicclasshtmlunitTest {
publicstaticvoidmain(String args[]){
WebDriver driver = newHtmlUnitDriver();
// Navigate to Google
driver.get("http://www.google.com");
// Locate the searchbox using its name
WebElement element = driver.findElement(By.name("q"));
// Enter a search query
element.sendKeys("trendnxt.blogspot.com");
element.submit();
// This code will print the page title
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
OUTPUT
PhantomJS
PhantomJS is a headless web browser using WebKit layout engine for rendering web pages and JavaScriptCore for executing scripted tests. The latest version of PhantomJS is an easy to install, stand-alone binary that doesn’t require installing Node.js or any other dependencies, and ships with its own ‘Ghost Driver’ for implementing the WebDriver Wire Protocol.
Ghost Driver
Ghost Driver is a pure JavaScript implementation of the WebDriver Wire Protocol for PhantomJS. It's a Remote WebDriver that uses PhantomJS as back-end.
Configuration and set up for PhantomJS and PhantomJS Driver
PhantomsJS
Download PhantomJS from below link
http://phantomjs.org/download.html
Extract the downloaded folder to Program files folder (means once you are extracted the phantomjs-xx.xx.xx-windows.zipfolder, copy the Phatomjs-xx. xx. xx-windows folder to C:\Program files)
PhantomsJS Driver
Download the PhantomJS driver from below link
http://mvnrepository.com/artifact/com.github.detro.ghostdriver/phantomjsdriver/1.1.0
click on 1.1.0 version link
Click on Download link
Writing your first test in Java for PhantomJS
1) Create a Java Project.
2) Associate below mentioned jar files
a) Latest Selenium-java client jar files
b) Latest PhantomJS Driver jar files
packagephantomjsproject;
importjava.io.File;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.phantomjs.PhantomJSDriver;
publicclass phantonjsexp1 {
publicstaticvoid main(String[] args) {
File file = newFile("C:/Program Files/phantomjs-2.1.1-windows/bin/phantomjs.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
WebDriver driver = newPhantomJSDriver();
driver.get("https://irctc.co.in");
WebElement element = driver.findElement(By.xpath("//input[@id='usernameId']"));
element.sendKeys("abced");
WebElement element1 = driver.findElement(By.xpath("//input[@class='loginPassword']"));
element1.sendKeys("fsafasf@");
WebElement element2 = driver.findElement(By.xpath("//input[@name='j_captcha']"));
element2.sendKeys("fsafasf@");
WebElement element3 = driver.findElement(By.xpath("//input[@id='loginbutton']"));
element3.click();
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
Output
Comments
Post a Comment