<img alt="" src="https://secure.agile365enterprise.com/790157.png" style="display:none;">

What is PhantomJS

author
By Deepshikha Singh Jul 12, 2017
What is PhantomJS
What is PhantomJS

PhantomJS is a headless browser, meaning a web browser without a graphical user interface, used for automating web page interaction. It is based on WebKit, the web browser engine. So it cannot render web pages but can act like a web browser. PhantomJS provides a JavaScript API to enable navigation, screen capture, user behaviour, assertions etc. It also supports touch events, making it relevant to mobile applications as well. 

Common use cases for PhantomJS 

  • Headless Testing - PhantomJS is a popular tool for running unit tests. It can simulate user behaviour like resource request and resource receipt without using the UI. It executes automated tests and displays results in the command line.
  • Page Automation - PhantomJS can load and manipulate web pages. It can automate a lot of QA tasks as by implementing web standards like DOM, CSS and JavaScript. You can set up scripts to trigger clicks or move from one page to another. It can respond to events like 'Page not found', 'Page closed' etc. It also helps to check sign-in issues.
  • Screen Capture - PhantomJS can get a screenshot of a web page with features to adjust the size of the screen and the exact area to be captured. It can get screenshots of HTML pages and various image formats like JPEG, GIF and PNG. It also supports Adobe Acrobat files.
  • Network Monitoring - PhantomJS can be connected to a web application when there is a request-response cycle. It can collect data about the website. This data can be re-formatted and viewed to analyse network behaviour and performance.
  • Client Side Testing - Most websites use heavy client side technologies. PhantomJS can be very efficiently used to execute client side scripts and render DOM as a web browser.

At Srijan, we’ve leveraged PhantomJS on two of our recent project:

IAG - One of the projects where PhantomJS has been extensively used is IAG wherein we used it with Behat to integrate with JS elements on the web page. Since Behat restricts us to interact with JS elements on a webpage, hence PhantomJs has become a necessity.

Also, Phantomjs helps to reduce the execution time thereby improving the performance.

OnCorps - PhantomJS is used with Codeception where we have a docker container based system. The PhantomJS server is running on a different container as compared to where the tests are running, so the headless browser stuff is done by PhantomJS. 

OnCorps uses Acquia for their code and it’s not very compatible with anything non-PHP. So, we had to run PhantomJS in a separate container.

Integrating Phantom with other software

As you can see, we’ve used PhantomJS in conjunction with other tools, and there can be numerous such examples:

  • Many testing tools like Mocha and Casper that are widely used are based on PhantomJS. 
  • You can have a test framework wherein the Javascript unit tests can be written in Jasmine. These can be executed using PhantomJS. This can be integrated into the continuous build environment like TeamCity.
  • PhantomJS can be easily used in a CI environment based on Jenkins.

Advantages of PhantomJS

  • It’s available on all major operating systems – Windows, Mac OS X and Linux
  • It’s easy to install and doesn’t require too many resources. It can start working without the use of third-party devices or external services. Development is easy. For example, while writing code, you can select the HTML element you want to access.
  • It can be used to create Cron jobs to test certain functions on the production website.
  • A lot of manual testing can be replaced by PhantomJS tests. So testing is faster and more efficient.

Disadvantages of PhantomJS

  • PhantomJS can be used to run unit tests on the code but it is not a testing framework
  • It’s not the best tool to automate functional testing or end-to-end testing
  • It’s used to attack websites in an automated manner as it does not require a UI

All in all, PhantomJS is a good tool for client-side testing. It can be used in a testing framework to reduce efforts of manual testing. But it cannot be the only tool used for a comprehensive test of the application. So, you can check out a combination Behat, Mink, and PhantomJS for your testing suite. Or use PhantomJS and Selenium to automate tests.

You can also check out our posts on working with Behat, and Selenium, and our webinar on how to manage QA tasks in an agile project. Hope you find them insightful.

Subscribe to our newsletter