PhantomJs is a lightweight headless web browser built on webKit.
Lets walk through those features, in a perspective of automated web 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.
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.
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 is nothing but a technical jargon for Web Page Automation.
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.