Headless Automation Using Selenium and PhantomJS Driver

DZone 's Guide to

Headless Automation Using Selenium and PhantomJS Driver

PhantomJS provides a way to run automated tests headlessly. In this tutorial, learn how to set up and configure PhantomJS and use it to start running tests.

· DevOps Zone ·
Free Resource

I started using the PhantomJS driver for headless automation because I 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, too, 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 if there are some other ways of integrating PhantomJS with Selenium. After quite a bit of research, I found out that PhantomJS driver is readily available with the Selenium library.

Before starting, I want everybody to understand what PhantomJS is. 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, and JSON. It is quite fast compared to running tests using the Selenium web driver. PhantomJS uses the WebDriver Wire Protocol implementation, which is achieved by GhostDriver running at the back end of PhantomJS.

It becomes much easier when using PhantomJS driver with Selenium, as a 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. 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 your project, you will see the following folder structure:



Step 3) By default, there will be a set of system jars present. You have to import the required Selenium jars in order to get started. Follow these screenshots:


Step 4) Now, the secret ingredient that will help you 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 the 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, newString[] {“--web-security=no”, “--ignore-ssl-errors=yes”});

I have used an absolute path to point to the PhantomJS binary execution file, but best practice is to use a relative path, as it would be more generic to a system.

Now we are good to run our automation test case. This is the view of my console where the test cases are running:


Your console, or the CLI where the tests are being executed by PhantomJS, starts acting as a browser console. This is the reason users start seeing browser console related errors while the tests are running. I see this as an added advantage, because if there is a fatal console error that may lead to someone cracking the system, the console errors can be reported earlier to ensure the protection of the application.

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

Here's how the PhantomJS driver log looks:


That is the gist of headless automation using PhantomJS. Please do provide your feedback or shoot me a mail. I will try to resolve your queries. Further, you can go through my GitHub link to understand its implementation 

automation, devops, javascript, phantomjs, selenium, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}