Headless Testing Using PhantomJS And Selenium Webdriver


PhantomJS is a SuperFast Headless browser i.e without a GUI (Graphical User Interface).There’s a case where we dont want to invoke a browser(Waste of time) and execute our testcases.In that case we can use this PhantomJS. It access Web Pages but doesn’t show them to the user. There are various advantages if we use PhantomJs.It is Very fast in execution, It also Captures the Screenshots, Native for DOM handling,
It also Handles cookies.
There are few steps to be followed to Configure PhantomJS.

1. Download PhantomJS Click here to download PhantomJS

2. We need to set the Path in your local box
Right click Computer and click propeties.
Click Advanced System Settings
Click Environment Variables button.
Select path in System Variables and click edit button.
Copy and Paste the location of Phantomjs folder.
ex: ‘F:\DollarWorkbook\Phantomjs’
Click Ok button.

Its done from the system End.Now we need to mention the phantomjs.exe path in our code.Please follow the below code

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.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class PhantomExample {

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

DesiredCapabilities phantomBeast = DesiredCapabilities.phantomjs();

phantomBeast.setJavascriptEnabled(true);

phantomBeast.setCapability(“phantomjs.binary.path”,”F:\\Dollar\\com.advancedSelenium\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe”);

phantomBeast.setJavascriptEnabled(true);

PhantomJSDriver driver = new PhantomJSDriver(phantomBeast);

driver.manage().window().setSize(new Dimension(1600,900));

driver.get(“http://www.facebook.com/”);

System.out.println(driver.getTitle());

driver.findElement(By.id(“email”)).sendKeys(“dollarWilli@gmail.com”);

driver.findElement(By.id(“pass”)).sendKeys(“Invallidlol”);

driver.findElement(By.id(“loginbutton”)).click();

File Ss=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

try {
FileUtils.copyFile(Ss, new File(“d:/Dollar.jpg”));

}
catch (IOException e) {

System.out.println(e.getMessage());

}
}

}

Now you see a screenshot in the mentioned path.

Advertisements

2 thoughts on “Headless Testing Using PhantomJS And Selenium Webdriver

  1. Akhil June 19, 2015 / 3:37 pm

    Thanks keep posting .

    Like

  2. sourav June 27, 2015 / 11:54 am

    Good post

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s