I'm not sure why PhantomJS is necessary and I'm hoping someone can help.
4 Answers
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 :)

- 6,656
- 4
- 34
- 65
-
1IMO this should be the top answer :p – nehem Sep 16 '14 at 01:17
-
this is the answer to the question. – libra Nov 11 '14 at 08:09
-
this is a much better answer – davidpm4 Feb 18 '15 at 20:19
-
2Thanks for being human about this answer. I actually gained something rather than sorting through technical jargon! – IIllIIll May 20 '16 at 14:09
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.

- 28,465
- 32
- 91
- 105

- 12,523
- 3
- 46
- 39
-
2Lol the category for network monitoring that you linked just says "no". Is that right? – Jose Luis Sep 25 '15 at 14:51
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.
-
1I really liked the offloading of heavy rendering example, thanks for mentioning this! – Leila Hamon Oct 04 '12 at 15:25
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.

- 28,465
- 32
- 91
- 105