Travis Tidwell

Web technology, software, and everything else that bores my wife..

Using Phantom.js With Node.js

Recently, I have had much interest in building web automation and testing tools using Node.js. The challenge, however, is when using Node.js for building tests and automation, your options are pretty slim when picking your headless browser. While Zombie.js is a decent browser, it uses JSDOM for its layout engine, whereas most of the web is ran on (or based off of) WebKit. This creates problems when trying to formulate accurate tests as well as benefit from the ongoing development into the WebKit engine. What really peaked my interest was the project called Phantom.js which is basically a headless WebKit browser that exposes a JavaScript API to interact with the browser.

Here is the problem… Phantom.js is not compatible with Node.js. However, there is a project that does expose the Phantom.js browser within the Node.js environment. This project is called Node Phantom, which does a great job at bringing PhantomJS to NodeJS. But, there is still another issue where the API to this library is not easy to use like Zombie.js.

That is why I built a library which leverages the Node-Phantom library, but brings in the Zombie.js API for ease of use. This library is called Zombie Phantom and I hope you enjoy it.

Also, please read the project description to get an idea of the differences between this library and the Zombie API before using.

Here is an example of how to use this library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var Browser = require('zombie-phantom');

var browser = new Browser({
  site: 'http://localhost:8888',
  addJQuery: false  // This page already has jQuery installed so use it...
});

browser.visit('/user/login', function() {
  browser.fill('#edit-name', 'admin', function() {
    browser.fill('#edit-pass', '123password', function() {
      browser.pressButton('#edit-submit', function() {
        console.log('You are logged in!');
        browser.close();
      });
    });
  });
});

Enjoy…

Comments