PhantomJs for Functional Automated testing


PhantomJs is a lightweight headless web browser built on webKit.

The interaction that happens in Phantomjs is just same as other browsers, but it renders the pages under the hood with no display for a human to see what’s happening. It can be controlled directly via JavaScript.

This page here neatly summarizes the use of PhantomJs

Lets walk through those features, in a perspective of automated web testing

Headless Testing

As I mentioned before one of the main purpose of PhantomJs is Headless testing.
PhantomJs itself is not a test framework, it can be used in conjunction with other test frameworks.

Headless testing is nothing but testing on a Headless browser. A browser with no visible UI.
What is Headless Browser ?
There are other Headless browser variants like HTMLUnit, Splash.. etc.

Automated testing using selenium with PhantomJs can be possible and I have blogged here while ago.

Screen Capture

Just that UI is not visible to us, it doesn’t mean that we wont be able to get screenshots, it can be done programatically via Js API or any test framework like WebDriver.

Network Monitoring

PhantomJs allows us to monitor the network traffic, like how ChromeDriver allows us to capture the Network console.
It allows us to do Sniffing of Requests and Responses data’s can be stored in a HAR format and using a HAR viewer we can convert into HTML to view the nice graph of performance. This can be termed as Client Side Performance testing. Exactly like how BrowserMobProxy allowed us to capture. I blogged here, on that too earlier.
A very useful feature.

DOM Manipulation

DOM Manipulation is nothing but a technical jargon for Web Page Automation.
Since the full page is rendered under the hood, DOM and CSS selectors selection will work well and any JavaScript evaluation can be done to scrape some data from the page.


Command Line Interface
PhantomJs allows us to be controlled via a command line. Very easily an script can be executed.
This is why, it helps the data scrappers to use it with no much technical knowledge.

Now, having gone through the main purposes of Phantom Js inline with the Documentation page.

It might sound exciting to have all these features, where the famous WebDriver supports only the browser automation but not the features like data collection in HAR, headless testing with fast turnaround time,

Let us now see about using PhantomJs for Automated Functional testing.

Failures and Flaky

Failures and Flaky tests are frequent problems that I hear about from different people who use PhantomJs.
Let me tell you a small two line story: A strange thing that I faced while executing a End-to-End test using PhantomJs is the failure in a function flow,whereas the same script would pass fine with other browsers like Chrome, Firefox.
When we run an automated build via CI systems like Jenkins, Travis, Bamboo.. using PhantomJs it fails and we manually debug it works fine, run the same script with other browsers and it works fine. Developers wont be looking into fixing the issues cause with an Headless testing, where the core objective is passed with physical browsers.

Automate a User Interaction

Automating a Human Interaction on a web page, is Automated web testing is all what we know and learnt about. So the main focus should be automating and running against the Physical browsers and not with Headless browsers. I am not against using PhantomJs here, but all am trying to convey is that run and execute your function end-to-end tests on a physical browser like a human.

Well, that’s my opinion 🙂 Everyone has their own perspective. I will leave it upto you guys to decide the best. I hope the above content helps in someway.

Nov 30th Update: Links and minor updation to contents
Reference: PhantomJs official docs.

1 thought on “PhantomJs for Functional Automated testing”

  1. Really nice post!!
    It is quite unique way to describe phantomjs for functional automated testing. In this blog we can also know key features of automated web testing. If anyone want to lean more, he/she can visit Automated Testing


Leave a Comment