PhantomJS
- 2 min read
- 4 chapters
Related on Lably
Through JavaScript, Open-source software and Netflix
History
PhantomJS was released January 23, 2011 by Ariya Hidayat after several years in development.
The first commit to the public project was in 2011.
The logo commonly used to pictorially refer to PhantomJS is a fluorescent blue ghost atop a black background. This refers to the lack of graphical user interface, or main body of the browser, making PhantomJS users seem like ghosts.
In March 2018, the development of PhantomJS was suspended due to lack of active contributions.
Sign in to keep reading
3 more chapters are free with a Lably account.
Usage
The PhantomJS JavaScript API can be used to open web pages, take screenshots, execute user actions, and run injected JavaScript in the page context. For example, the following code will open Wikipedia and, upon loading, will save a screenshot to a file and exit.
<span id="mwKA"></span><span class="nx" id="mwKQ">console</span><span class="p" id="mwKg">.</span><span class="nx" id="mwKw">log</span><span class="p" id="mwLA">(</span><span class="s1" id="mwLQ">'Loading a web page'</span><span class="p" id="mwLg">);</span> <span class="kd" id="mwLw">var</span><span class="w" id="mwMA"> </span><span class="nx" id="mwMQ">page</span><span class="w" id="mwMg"> </span><span class="o" id="mwMw">=</span><span class="w" id="mwNA"> </span><span class="nx" id="mwNQ">require</span><span class="p" id="mwNg">(</span><span class="s1" id="mwNw">'webpage'</span><span class="p" id="mwOA">).</span><span class="nx" id="mwOQ">create</span><span class="p" id="mwOg">();</span> <span class="kd" id="mwOw">var</span><span class="w" id="mwPA"> </span><span class="nx" id="mwPQ">url</span><span class="w" id="mwPg"> </span><span class="o" id="mwPw">=</span><span class="w" id="mwQA"> </span><span class="s1" id="mwQQ">'http://en.wikipedia.org/'</span><span class="p" id="mwQg">;</span> <span class="nx" id="mwQw">page</span><span class="p" id="mwRA">.</span><span class="nx" id="mwRQ">open</span><span class="p" id="mwRg">(</span><span class="nx" id="mwRw">url</span><span class="p" id="mwSA">,</span><span class="w" id="mwSQ"> </span><span class="kd" id="mwSg">function</span><span class="w" id="mwSw"> </span><span class="p" id="mwTA">(</span><span class="nx" id="mwTQ">status</span><span class="p" id="mwTg">)</span><span class="w" id="mwTw"> </span><span class="p" id="mwUA">{</span> <span class="w" id="mwUQ"> </span><span class="nx" id="mwUg">console</span><span class="p" id="mwUw">.</span><span class="nx" id="mwVA">log</span><span class="p" id="mwVQ">(</span><span class="s1" id="mwVg">'Page loaded'</span><span class="p" id="mwVw">);</span> <span class="w" id="mwWA"> </span><span class="nx" id="mwWQ">page</span><span class="p" id="mwWg">.</span><span class="nx" id="mwWw">render</span><span class="p" id="mwXA">(</span><span class="s1" id="mwXQ">'wikipedia.org.png'</span><span class="p" id="mwXg">);</span> <span class="w" id="mwXw"> </span><span class="nx" id="mwYA">phantom</span><span class="p" id="mwYQ">.</span><span class="nx" id="mwYg">exit</span><span class="p" id="mwYw">();</span> <span class="p" id="mwZA">});</span>Ecosystem
PhantomJS became valuable enough that similar projects adopted the API as a standard way of interacting with headless browsers. SlimerJS provided a PhantomJS-like API, but on top of Mozilla's Gecko browser engine rather than WebKit. Similarly, trifleJS targeted the MSHTML engine of Internet Explorer.
Shortly after the release of PhantomJS, Nicolas Perriault wrote CasperJS, a suite of libraries on top of PhantomJS that extend its capabilities as a client for automated web page testing, allowing users to create BDD tests leveraging PhantomJS. The final release of CasperJS was in 2017.
Yahoo! developed a version of YSlow that leverages PhantomJS to gather performance metrics for websites.
Users
Several notable companies have used PhantomJS.
- Twitter used QUnit and PhantomJS for unit testing as of 2015.
- LinkedIn used PhantomJS based tools for performance testing as of 2011.
- Netflix used Sketchy, a headless browser built with PhantomJS, to understand what it's doing without having to visit the site as of 2014.
- Time Warner Cable used PhantomJS with CoffeeScript, Jasmine, and JUnit XML for Jenkins continuous integration as of 2012.

