$ npm install phantom-proxy
PhantomJs is an incredibly useful tool for functional and unit testing. PhantomJs runs in its own process, making it difficult to drive from node. Phantom-proxy solves this problem, allowing you to fully drive phantomjs from node.
npm install phantom-proxy
var phantomProxy = require(phantom-proxy);
phantomProxy.create({}, function (proxy) {
var page = proxy.page;
page.open('http://www.w3.org', function () {
page.waitForSelector('body', function () {
console.log('body tag present');
phantomProxy.end();
});
});
});
use this method to create an instance of the phantom proxy objects. The return value will be an object with a page proxy and a phantom proxy. These properties correspond to the phantom and webpage objects on the native phantom API.
When this method is called, a new phantomjs process is spawned. The new phantomjs process creates a mongoose webserver on localhost:1061. All subsequent communication with phantom occurs via http requests.
Create accepts an options object as the first parameter. This argument is optional. Properties and their default values are listed below.
var defaultoptions = {
'ignoreSslErrors':true,
'localToRemoteUrlAccessEnabled':true,
'cookiesFile':'cookies.txt',
'diskCache':'yes',
'loadImages':'yes',
'localToRemoteUrlAccess':'no',
'maxDiskCache':'50000',
'outputEncoding':'utf8',
'proxy':'0',
'proxyType':'yes',
'scriptEncoding':'yes',
'webSecurity':'yes',
'port':1061
};
It is important to call end when you are done using the proxy to terminate the underlying phantomjs process that is being driven.
var phantomProxy = require('phantom-proxy').create(function(proxy){
var page = proxy.page,
phantom = proxy.phantom;
//do some stuff...
//...
//call end when done
phantomProxy.end();
});
The phantom object corresponds to the phantom object in the native phantomJs API.
The page object corresponds to the webpage object in the native phantomJs API.
sets property on page object
//set viewport size for browser window
proxy.page.set('viewportSize',
{ width:320, height:480 }, function (result) {
console.log(result.toString().cyan);
worldCallback.call(self);
});
Opens a webpage with url and callback function arguments.
var phantomProxy = require('phantom-proxy').create({}, function(proxy){
var page = proxy.page,
phantom = proxy.phantom;
page.open('http://www.w3.org', function(){
console.log('page now open');
//close proxy
phantomProxy.end();
});
});
Polls page for presence of selector, executes callback when selector is present.
var phantomProxy = require('phantom-proxy').create({}, function(proxy){
var page = proxy.page,
phantom = proxy.phantom;
page.open('http://www.w3.org', function(){
page.waitForSelector('body', function(){
console.log('body tag present');
//close proxy
phantomProxy.end();
});
console.log('page now open');
});
});
Renders a image of browser.
var phantomProxy = require('phantom-proxy').create({}, function(proxy){
var page = proxy.page,
phantom = proxy.phantom;
page.open('http://www.w3.org', function(){
page.waitForSelector('body', function(){
console.log('body tag present');
page.render('myimage.png', function(){
console.log('saved my picture!');
//close proxy
phantomProxy.end();
});
});
console.log('page now open');
});
});
Returns a base64 representation of image.
Executes functionToEvaluate in phantomJS browser. Once function executes, callbackFn will be invoked with a result parameter. The Third and sebsequent arguments represent optional parameters which will be passed to the functionToEvaluate function when it is invoked in the browser.
phantomProxy = require('phantom-proxy');
phantomProxy.create({}, function (proxy) {
proxy.page.onConsoleMessage = function (event) {
console.log(JSON.stringify(event));
};
});
The short answer is that phantom-proxy has a better implementation. Other drivers seem to use a side effect of alerts to communicate with phantomjs, which unfortunately is not a good long term solution. Also all the other libraries I have seen do not have a fully implemented the evaluate function - you can't pass additional arguments to the client side function. This is a big problem and was the main motivation for creating this package.
© 2010 - cnpmjs.org x YWFE | Home | YWFE