Quick Start of running Selenium WebDriver against PhantomJS in Java on Windows (Plain Java)

3

25. February 2014 by Michael Wowro

This is the quickstart without Maven, without IDE – just plain Java. For the quickstart with Maven look here

The browser with the probably highest speed and lowest memory consumption in Selenium world: the headless PhantomJS

[25.Feb.2014]: Ghostdriver is not ripe yet due to lack of enough stable maintainers: https://github.com/detro/ghostdriver/issues/140

[Note]: The development of Selenium and GhostDriver (which is the bridge between PhantomJS and Selenium) isn’t synchronized yet. So maybe the most actual versions of Selenium Server and GhostDriver may not work together. An (older) combination that fits:

* PhantomJS 1.9.2
* GhostDriver 1.0.4
* Selenium 2.34

1.) If you haven’t done yet, download the Java JDK from the download page (take the most actual Java Platform (JDK)). Execute it and follow the wizard. After that, set PATH.

2.) Download Selenium-Server-Standalone(!) 2.34.0 from

http://code.google.com/p/selenium/downloads/list?can=1&q=

On my notebook I have put it in C:\Users\IT Kosmopolit

3.) Download phantomjs-1.9.2-windows.zip here and extract it to C:\Program Files (x86)

4.) Add C:\Program Files (x86)\phantomjs-1.9.2-windows to the PATH-variable

5.) Download phantomjsdriver 1.0.4 from

http://mvnrepository.com/artifact/com.github.detro.ghostdriver/phantomjsdriver/1.0.4

On my notebook I have put it in C:\Users\IT Kosmopolit

6.) Save the following codesnippet as Example.java. On my notebook I have put it in C:\Users\IT Kosmopolit

Sample Code

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;

public class Example {

public static void main(String[] args) {
WebDriver driver = new PhantomJSDriver();
driver.get("http://www.google.com");

WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!");
element.submit();

System.out.println("Page title is: " + driver.getTitle());

driver.quit();
}
}

7.) Open the Command Prompt and compile with javac:

javac -classpath "C:\Users\IT Kosmopolit\selenium-server-standalone-2.34.0.jar;C:\Users\IT Kosmopolit\phantomjsdriver-1.0.4.jar" Example.java

8.) Execute in the Command Prompt

java -classpath .;"C:\Users\IT Kosmopolit\selenium-server-standalone-2.34.0.jar;C:\Users\IT Kosmopolit\phantomjsdriver-1.0.4.jar" Example

9.) If you get a result like this, you’re done

success

Advertisements

3 thoughts on “Quick Start of running Selenium WebDriver against PhantomJS in Java on Windows (Plain Java)

  1. shaibal says:

    great job!! thanks really appreciate your help.

  2. shaibal says:

    i have one question about the capability of PhantomJS to take snapshot

    Example code:

    var page = require(‘webpage’).create();

    page.open(‘http://mywebpage/’, function ()
    {

    page.render(‘//location/to/save/the/snapshot.jpg’);

    phantom.exit();

    });
    error: ‘webpage’ invalid character constant

    I believe webpage is a primary module in phantomjs and i am using version 1.9.1. How can i check the available modules in phantomjs. Note that i have install it on mac via brew.

    command : brew install phantomjs (i got 1.9.1 version)

    i am using eclipse +junit+phantomjs

    so how can i let eclipse know that there is a module called webpage for phantomjs.

    How to inject a module if it is not present? and if i cannot inject,how can i install a new version which does have the module?

    thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

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

%d bloggers like this: