Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 606
Leila Hamon Avatar asked Oct 03 '12 14:10

Leila Hamon


2 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 :)

like image 197
anotherdave Avatar answered Sep 17 '22 18:09

anotherdave


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.

like image 44
Ariya Hidayat Avatar answered Sep 19 '22 18:09

Ariya Hidayat