PhantomJS 1.7, Blazing Star, was released on September 22, 2012. It added module system support (modelled after CommonJS), better cookies handling, additional input events, and other small improvements.
Module system
There has been module system support for a while, however it works only for internal modules. In this release, that support is extended for generic module written in JavaScript or CoffeeScript. The construct for the module system is modelled after CommonJS Modules 1.1.
As an example, supposed there is a script universe.js
which contains the following code:
exports.answer = 42; exports.start = function () { console.log('Starting the universe....'); }
This module can be used in another script like the following:
var universe = require('./universe'); universe.start(); console.log('The answer is', universe.answer);
With this module support, a long PhantomJS script can be broken down into smaller files and thus it becomes more maintainable.
Cookies handling
Support for cookies has been added in the previous version. In this release, the implementation is significantly improved in order to make it behave as similar as possible in a real web browser. The cookies are stored in a global container (“cookiejar”) which can be serialized and modified easily.
The following simple example shows every single cookie set when accessing a web site:
var page = require('webpage').create(); phantom.cookiesEnabled = true; page.open('http://m.bing.com', function () { console.log(JSON.stringify(phantom.cookies, null, 2)); phantom.exit(); });
More input events
PhantomJS can send input events to a web page. This not a DOM event, it is more like a real user input in a web browser.
In this version, a mouse event can now explicitly specify the button corresponding to the event: middle
or right
(otherwise it is left by default). In addition, doubleclick
event type is also supported. The following code fragment demonstrates sending a right-click:
page.open(url, function () { page.sendEvent('mousepress', 50, 100, 'right'); });
Beside mouse events, finally you can also trigger keyboard-related events: keyup
, keydown
, and keypress
. For any of these events, a character or a string needs to be passed as in the following code fragment:
page.open(url, function () { page.sendEvent('keypress', 'A'); // send one char page.sendEvent('keypress', 'PhantomJS'); // send a sequence of characters });
New features
Improvements
Known Issues on Microsoft Windows
Back to all releases.