Why
Headless Browser?
There
are times when you don't want a browser popping each time you execute the test.
1. Lets assume our test running in the
continuous integration environment Like
- jenkins
- Banboo
Here all test will
run on server.So no need any Physical things need to be done like (opening
browser).We get result in Logs file at end of test.In this situation we no need
to open browser .
2.We made it to run locally also.
3.Headless browser is very fast to excute.
In this Program:
·
Navigating to http://automationplace.blogspot.in
·
Verifying the title
Without Headless Browser
Sourcecode :
import
java.util.concurrent.TimeUnit;
import org.junit.Test;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;
public class test_headlessbrowser
{
@Test
public void headlessBrowser()
{
WebDriver
driver=driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
driver.get("http://www.automationplace.blogspot.in");
String title=driver.findElement(By.xpath("//*[@id='header-inner']/div[1]/h1")).getText();
//this
will verify my page title
System.out.println(title);
Assert.assertTrue(title.contains("Automationplace"));
}
}
Note : The
Execution is slow comparing to Headless
Browser
The Execution will take 26.591 sec.
WithHeadless Browser:
To run in headless
browser we need to change only one thing in the above code.
i.e)WebDriver driver=driver = new HtmlUnitDriver();
The
rest of the things is same.
Source code :
import
java.util.concurrent.TimeUnit;
import org.junit.Test;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import
org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;
public class test_headlessbrowser
{
@Test
public void headlessBrowser()
{
WebDriver
driver=driver = new HtmlUnitDriver();
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
driver.get("http://www.automationplace.blogspot.in");
String title=driver.findElement(By.xpath("//*[@id='header-inner']/div[1]/h1")).getText();
//this
will verify my page title
System.out.println(title);
Assert.assertTrue(title.contains("Automationplace"));
}
}
If you Run the above code
The Execution will take only 7.738 sec
Hope you like this post.