An Introduction to PhantomJS

Phantomjs is a great tool for end-to-end tests of your application. It provides a headless browser based on webkit that allows your tests to navigate through the web application.

At Codeship we use its capybara integration, but Phantomjs can be used for lots of purposes. Throughout the lifetime of our codebase we have tried various Capybara drivers to find the one that suits our test environment and workflow.

We started off with akephalos, moved on to Selenium and switched that for capybara-webkit. All of them have their benefits, but also their quirks.

Selenium broke frequently when new Firefox versions were released which were incompatible with older versions of the selenium-webdriver gem. Another issue was that Selenium had problems with links covered by modal panels. One of our customers experienced the same behavior in capybara-webkit. PhantomJS correctly failed and provided the right error messages.

Finally we started using PhantomJS with great success. PhantomJS’ main advantage for us was that it fails tests on JavaScript errors. Neither Selenium nor capybara-webkit support this, but it is absolutely crucial. In our team everyone is allowed to deploy new code to the live system so we really need to make our tests hard and safe. Making sure there are no JavaScript errors on your page is mandatory. We had a handful of instances where a small change in JavaScript broke all of our JavaScript code.

Getting started with PhantomJS

Getting started with Phantomjs and Capybara is easy. Make sure to download the latest version from the Phantomjs website and follow the installation instructions on their install page.

Following is a minimal setup gist to get you started. The Poltergeist gem integrates PhantomJS with Capybara and provides a lot of great extensions. Take a look at their customization options to get a better understanding of what you can do.

Here is the Capybara config with the parallel_tests gem setup we use for Codeship:

Additionally this raises JavaScript errors and increases the Phantomjs timeout.

Codeship – A hosted Continuous Deployment platform for web applications

Conclusion

Give PhantomJS a try for your test environment! It is a stable, easy to use and a powerful friend.

Subscribe via Email

Be sure to join 13,643 subscribers of our newsletter to receive updates on software development best practices, Continuous Delivery and tips and tricks to start shipping your product faster.

Join the Discussion

Leave us some comments on what you think about this topic or if you like to add something.

  • http://daemons.net/ claymation

    Is it easy to use PhantomJS in a Codeship project test suite?

    • https://www.codeship.io/ Florian Motlik @codeship

      Absolutely. We’ve got phantomjs preinstalled and in the PATH, so you can use it right away.

  • Arnold

    Could anyone tell if Phantomjs supports mobile testing ?

    • https://www.codeship.io/ Florian Motlik @codeship

      We haven’t tested it with Mobile so far. You can resize easily to simulate a mobile experience though: http://jonathanmh.com/automate-mobile-testing-phantom-js/

      • kireeti

        Thanks Florian..Can u please tell me
        1) How to test a web app(html website) using phantom js ?
        2) can we automate the same for mobile versions of that website ?
        3) do we need additional tools to automate using Phantom js

  • kireeti

    Can u please tell me

    1) How to test a web app(html website) using phantom js ?

    2) can we automate the same for mobile versions of that website ?

    3) do we need additional tools to automate using Phantom js

    • http://gosia.migdal.pl/ Małgorzata Migdał

      Hi Kireeti! If you would like to see how to test web apps using PhantomJS, please take a look at the examples which I put in this simple pad: http://phantomjs-pad.ozaur.com/ (developed by me:)). You do not need any additional tools to automate using PhantomJS. You can test mobile versions of the websites (just be specifying view ports sizes). If you want to have a chat etc. to get to know more about PhantomJS please let me know.