Running WebDriver Test In Headless Browser Using PhantomJS GhostDriver

Earlier we have configured PhantomJS GhostDriver with eclipse In previous post to run Selenium WebDriver test In headless browser. Now we are all set to execute sample WebDriver test In eclipse using PhantomJS GhostDriver. I have created sample test as bellow which will be executed In headless browser.

We will use DesiredCapabilities to set phantomjs.exe executable file's path and then we will pass It with PhantomJSDriver Initialization as shown In bellow example.

Note : Please set proper path of phantomjs.exe file In bellow given examples.

package Testing_Pack;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class phantomjs {
 WebDriver driver;
 
 @BeforeTest
 public void setup() throws Exception {
  //Set phantomjs.exe executable file path using DesiredCapabilities.
  DesiredCapabilities capability = new DesiredCapabilities();  
  capability.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "E:/phantomjs-2.0.0-windows/bin/phantomjs.exe");
  driver = new PhantomJSDriver(capability);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);  
 }
 
 @Test
 public void phantomTest() throws IOException{
  driver.get("http://only-testing-blog.blogspot.com/2014/04/calc.html");
  //Get current page title using javascript executor.
    JavascriptExecutor javascript = (JavascriptExecutor) driver;
    String pagetitle=(String)javascript.executeScript("return document.title");  
    System.out.println("My Page Title Is  : "+pagetitle);    
  driver.findElement(By.xpath("//input[@id='2']")).click();
  driver.findElement(By.xpath("//input[@id='plus']")).click();
  driver.findElement(By.xpath("//input[@id='3']")).click();
  driver.findElement(By.xpath("//input[@id='equals']")).click();
  String sum = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("****** Sum Is : "+sum+" ******");  
 }
}

When you run above example In eclipse, It will execute test In headless PhantomJS GhostDriver and print page title and sum result In console as bellow.


Capturing page screenshot
Question : Is It possible to capture page screenshot If your test Is executed In headless browser using PhantomJS? Answer Is Yes. You can get page screenshot when test Is being executed In headless browser.

Bellow given example will capture page screenshot and save file with name "Test.jpeg" In D: drive.
package Testing_Pack;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class phantomjs {
 WebDriver driver;
 
 @BeforeTest
 public void setup() throws Exception {
  //Set phantomjs.exe executable file path using DesiredCapabilities.
  DesiredCapabilities capability = new DesiredCapabilities();  
  capability.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "E:/phantomjs-2.0.0-windows/bin/phantomjs.exe");
  driver = new PhantomJSDriver(capability);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);  
 }
 
 @Test
 public void phantomTest() throws IOException{
  driver.get("http://only-testing-blog.blogspot.com/2014/04/calc.html");
  //Get current page title using javascript executor.
    JavascriptExecutor javascript = (JavascriptExecutor) driver;
    String pagetitle=(String)javascript.executeScript("return document.title");  
    System.out.println("My Page Title Is  : "+pagetitle);    
  driver.findElement(By.xpath("//input[@id='2']")).click();
  driver.findElement(By.xpath("//input[@id='plus']")).click();
  driver.findElement(By.xpath("//input[@id='3']")).click();
  driver.findElement(By.xpath("//input[@id='equals']")).click();
  String sum = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("****** Sum Is : "+sum+" ******");
  
  //To capture page screenshot and save In D: drive.
  File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  FileUtils.copyFile(scrFile, new File("D:\\Test.jpeg"),true);
 }
}

Execute above test and open D: drive when execution completed. There will be "Test.jpeg" In D: drive. This way, You can capture page screenshot for phantomjs driver test case.

No comments:

Post a Comment