Why would a developer ever need PhantomJS as opposed to just using some testing framework?
I'm not sure why PhantomJS is necessary and I'm hoping someone can help.
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 :)
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.
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.