Thursday, October 17, 2013

Speedup Your Selenium Test with PhantomJS

I was searching some way to speedup my WebDriver tests and this is what I came across, PhantomJS. It took awhile to understand how it works but it's worth spending time as I can say it's more than 50% faster when your run your test with PhantomJS. So,

What is PhantomJS?

PhantomJS is a headless WebKit scriptable with a JavaScript API. It's okay if you didn't understand it by this single line. Let me explain it to you.
Webkit is a browser engine which allows web browser to render pages, and it is used by many streamline browser (I guess Firefox and Google chrome both uses this). Now what is this headless then? Headless mean you won't be able to see any GUI component of the browser.

So Why PhantomJS is faster ?

This is the second question you might come up with. That if it is the same engine that many browser uses then how my test cases will execute faster if I will run them on PhantomJS.
The reason is, it just process data (request and response) it doesn't draw canvas.

We have had enough talk, now let's compare the result.

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.testng.annotations.Test;

public class TestPhantoJS {

 /**
  * @author Gaurang_Shah
  * Following test will search the keyword on google 
  * and will print all websites on first page.
  */
 @Test
 public void GoogleSearch(){
  WebDriver driver = new PhantomJSDriver();
//  WebDriver driver = new FirefoxDriver();
  driver.get("http://google.com");
  driver.findElement(By.name("q")).sendKeys("phantomjs");
  driver.findElement(By.name("q")).submit();
  List<WebElement> sites = driver.findElements(By.xpath("//cite"));
  System.out.println(driver.getTitle());
  int index=1;
  for(WebElement site: sites){
   String siteName=site.getText();
   if(!siteName.equalsIgnoreCase(""))
    System.out.println(index+++":--"+site.getText());
  }
  driver.close();
 }
}
On my machine when I am running above test with PhantomJSdriver and FirefoxDriver following is the result.
FirefoxDriver - ~28 seconds
PhantomJSDriver - ~13 seconds
Pretty Fast. Right ??

Let Me Move My all Tests to PhantomJSDriver 

this is the first thought that my come up in your mind after this result. Right ???, however I wouldn't advice you to do that. And there are reason to this as well as mention below.
PhantomJS is just a headless WebKit which uses JavaScript, however it uses GhostDriver to run your test cases used webdriver. And it's in intermediate stage and you might feel bit trouble with someone the WebDriver API.

What is GhostDriver?? 

I am sure you must have this question in mind. Let me try to explain it to you. It's a Webdriver wire protocol in simple javascript for PhantomJS. (what the hell ??? ) ohh. let me make it more simple. GhostDriver is not but some kind of communicator between your Webdriver Bidning and PhantomJS. PhantomJS comes with the GhostDriver inbuilt, however it's available separately as well.

2 comments:

Anonymous said...

Hi,

I reposted this on the subreddit for PhantomJS. Please consider posting there any time you have future updates regarding this technology.

Thanks,
http://www.reddit.com/r/phantomjs/

satish said...

HI,

Can you explain drag and drop using phantomjs and selenium
please do the needful

Thanks,
Satish

Post a Comment