Headless automation using Selenium + PhantomJS driver

So, this blog post of my would be based on headless automation testing. First I would like to explain my blog readers what is meant by headless browsing? Headless as the word suggest without being seen i.e when running automation tests users would not see any direct browser interaction but most of the functionalities would run from the back end. So basically a headless browser is a web browser without a graphical user interface. So google started using headless browsing from 2009 in order to index contents of websites using ajax.

I started using phantomjs driver for headless automation because I have initially used casperjs to carry out automation of few test cases. Now casperjs is just a wrapper over phantomjs to extend its capability. It was quite easy to implement and interesting to, seeing it run in a headless way. But as I am new to JavaScript it was a bit time consuming for me to learn the implementation of casperjs along with the implementation of JavaScript. That is when I started researching thinking about this fact that if there are some other ways of integrating phantomjs with selenium. After quite a bit of research I found out that phantomjs driver is itself readily available with the Selenium library.

But before starting I want everybody to understand what is phantomjs. PhantomJs is a webkit which runs headless with an inbuilt javascript API. It has fast and native support for various web standards such as DOM handling, CSS selector, JSON and etc. It is quite fast as compared to running tests using selenium web driver. PhantomJS uses the WebDriver Wire Protocol implementation which is achieved by GhostDriver running at the back end of phantomjs.

It becomes quite easier when using phantomjs driver with Selenium. As the person can use his own implementation technique to automate test suites using phantomjs. For example I used Java to implement the test scenarios with phantomjs as a browser to run the desired test cases.


      1. You will be needing the binary executable file of phantomjs. Make sure the phantomjs version is above 2.

      2. Java Development Kit installed in the machine.

      3. Eclipse IDE (this is not mandatory), you can also run your test cases from your console.

      4. Selenium jars.

      5. Test-NGI used Test-NG to run the test cases. People can also use the normal java implementation to run the test cases.

Let me get you to the automation part now.

Required steps

Step-1: Open Eclipse IDE and create a Java project.

The following screen will appear



Step-2: Create the required project. Now when you click on you project you will see the following folder structure.



Step-3: By default there would be a set of system jars present. You would have to import the required selenium jars in order to get started. Follow the required screenshots


Now the secret ingredient that would help you all start running your automation using phantomjs.

Capabilities caps = new DesiredCapabilities();

((DesiredCapabilities) caps).setJavascriptEnabled(true);

((DesiredCapabilities) caps).setCapability(“takesScreenshot”, true);

((DesiredCapabilities) caps).setCapability(




driver = new PhantomJSDriver(caps);

return driver;

Now as you see I have used Capabilities class and its required methods to configure the driver instance as to what configurations the driver should possess in order begin execution on the browser side. Like in the above snippet I have set the desired capability to get the path of the binary phantomjs execution file. Also I have set phantomjs to take screenshots. You can also set the capability to ignore all SSL alerts appearing by using this line of code

((DesiredCapabilities) caps).setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {“–web-security=no”, “–ignore-ssl-errors=yes”});

Although I have used absolute path to point to the phantomjs binary execution file. But best practice is to use relative path as it would be more generic to a system.

And we are good to run our automation test case.

This is the view of my console where the test cases are running.


So a few points with respect to the console where you see the test cases running

  1. Your console or the CLI where the tests are being executed by phantomjs starts acting as a browser console.

  2. This is the reason why users start seeing browser console related errors while the tests are running. I see this as an added advantage. As, because if there is a fatal console error that may lead to someone cracking the system, in that case the console errors can be reported earlier to ensure the protection of the application.

  3. Besides this there is a log also known as phantomjsdriver log which separately guides the user with the browser behaviour, such as page settings for each session, what are the errors in case there is any failure in running the test cases etc.

Heres how the phantomjs driver log looks like


So there is the gist of headless automation using phantomjs. Please do provide your feedback and any doubt shoot me a mail. I will try to resolve your queries. For further, you can go through my github link to understand the implementation https://github.com/Corefinder89/Selenium-PhantomJSDriver

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.