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.

Tuesday, October 8, 2013

Webdriver: RobotFramework with Java - Part 1

There are so many posts available which states how to install/setup and configure robotframwork in python, however i didn't find any good posts which states how to configure robotframework in Java. So decided to write a blog.

Install/Setup Robot Framework 

To run webdriver test with RoboFramework in java you require following two jar files. 

Download the above two files and put that into some directory. 

Sample Test Case

Create the testcase.txt as mention below into the same directory in which you downloaded JARs
***Setting***
Library    Selenium2Library

*** Test Cases ***
Get Current Browser Test
    Open Browser  http://google.com

Run Test Case

To run the RobotFramework test case you need to set the CLASSPATH. Please add above jar files into your classapth and run the following command 
java org.robotframework.RobotFramework run testcase.txt

Verify Results

Results will be located into same directory in which you have created test case. 

Tuesday, October 1, 2013

WebDriver: TestNG with ANT

Few posts back I explained how to use Maven with Webdriver and TestNG. Now let's see how to use ANT with WebDriver and TestNG.
<project name="WebDriverAntTestNG" default="run" basedir=".">
 
 <path id="libs">
  <!-- Include all the external Jar files -->
  <fileset dir="${basedir}\src\jars">
   <include name="*.jar"/>
  </fileset>
  <!-- Include all the compile classes -->
  <fileset dir="bin">
   <include name = "**/*.class"/>
  </fileset>
  <pathelement path="${basedir}\bin"/>
  </path>
 
 <target name="run">
    <antcall target="init"/>
    <antcall target="compile"/>
    <antcall target="runTests"/>
  </target>
 
   <!-- Delete old data and create new directories -->
  <target name="init" >
   <echo>Initlizing...</echo>
   <delete dir="bin" />
    <mkdir dir="bin"/>
    <delete dir="report" />
    <mkdir dir="report"/>
    
  </target>
 
 <!-- Complies the java files -->
 <target name="compile">
    <echo>Compiling...</echo>
    <javac includeantruntime="false" debug="true" srcdir="src" destdir="bin"   classpathref="libs" />
  </target>

 <!-- Runs the file and generates Reportng report -->
 <target name="runTests" description="Running tests" >
  <echo>Running Tests...</echo>
    <taskdef resource="testngtasks" classpathref="libs"/>
    <testng outputDir="report"
    haltonfailure="true"
    classpathref="libs"
      >
   <classfileset dir="bin" includes="**/*.class" />
    </testng>
  </target>

</project>

Targets Explained:


Run: This is main target, which will call all the below target in order.
Init: This target will delete the Bin an Report directory and will crate again in order to clean. 
Compile: This target will compile all the java classes. 
runTests: This target contain the TestNGTask which will all the testng tests.