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.
- 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.
- 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.