Why would a developer ever need PhantomJS as opposed to just using some testing framework?

JavascriptTestingCoffeescriptPhantomjs

Javascript Problem Overview


I'm not sure why PhantomJS is necessary and I'm hoping someone can help.

Javascript Solutions


Solution 1 - Javascript

JavaScript that requires a DOM needs someplace to run, normally in the browser. So test frameworks often fire up a browser which they then control in order to run the tests & carry out asserts. The test frameworks themselves often can't control the browser directly, so you end up with three layers to your test:

  • Framework: E.g. Jasmine
  • Driver: E.g. Selenium
  • Browser: E.g. Firefox

Simply, PhantomJS is a browser which runs headlessly (i.e. doesn't draw out the the screen). The benefits that brings is speed — if you're controlling an actual programme on your computer, you've a certain overhead in booting up the browser, configuring a profile etc.

PhantomJS is much faster at this (I've seen tests run in less than 1/3 the time). The only trade-off is that you cannot see your tests running, though that's not much of an issue for an automated suite! You could also take screenshots if needed (e.g. on failures). If you really wanted to, you could take screenshots at 24fps & send to ffmpeg to render out a video of your tests, though you may undermine your speed savings then :)

Solution 2 - Javascript

This is a common misunderstanding. Spending some time with the documentation usually clears the confusion. For example, PhantomJS is often used as a headless test runner for tests written in dozens of frameworks: Buster.JS, Capybara, Mocha, Jasmine, QUnit, WebDriver, YUI Test, and many more. As you can see here, PhantomJS does not replace those frameworks at all.

Of course, there are many other uses of PhantomJS, everything from programmatically capture web page to automated network monitoring. There is a list of various blog posts and articles which demonstrate different reported use cases of PhantomJS.

Solution 3 - Javascript

PhantomJS is not meant to replace a testing framework, it will work in conjunction with one.

For example, here's a random example PhantomJS used in conjunction with Jasmine.

Also note that PhantomJS isn't just for testing, you can use it for things like screen-scraping or for off-loading heavy rendering load (say, for CPU-challenged mobile browsers) onto the server, as in this example.

Solution 4 - Javascript

Another use case that has not been mentioned here is rendering a webpage and generating image or PDF snapshots of that page.

For example you could render an invoice, report, document, ... as HTML webpage and render it to PDF to send it to your end user as downloadable document.

Or maybe you want screenshot thumbnails of a webpage? Another possible use case.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeila HamonView Question on Stackoverflow
Solution 1 - JavascriptanotherdaveView Answer on Stackoverflow
Solution 2 - JavascriptAriya HidayatView Answer on Stackoverflow
Solution 3 - JavascriptmanzoidView Answer on Stackoverflow
Solution 4 - JavascriptBlaMView Answer on Stackoverflow