phantom-proxy
A set of proxy objects for driving phantomjs from node
Last updated 14 years ago by sheebz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install phantom-proxy 
SYNC missed versions from official npm registry.

phantom-proxy

Provides a proxy interface for phantomjs which runs inside of node. Now you can run phantom and access the full api from within nodejs.

Overview

PhantomJs is an incredibly useful tool for functional and unit testing. Problem is that it runs in its own process which is outside of node, so you are basically out of luck if you want to integrate with phantomjs from within node. There are some other libraries - phantom-node, node-phantom, that address the same problem.

Installation

npm install phantom-proxy

Usage

API

phantomProxy object

createProxy(options, callbackFn)

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.

require('phantom-proxy').createProxy({}, function(proxy){
  var page = proxy.page,
  phantom = proxy.phantom;
  
});

phantom Object

The phantom object corresponds to the phantom object in the native phantomJs API.

exit(callbackFn)

Terminates phantom webserver and destroys proxy.

require('phantom-proxy').createProxy({}, function(proxy){
  var page = proxy.page,
  phantom = proxy.phantom;
  
  page.open('http://www.w3.org', function(){
    console.log('page now open');
    phantom.exit(function(){
      console.log('phantom proxy is now offline');
    });
  }); 
});

Page Object

The page object corresponds to the webpage object in the native phantomJs API.

set(propertyName, propertyValue, callbackFn)

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);
                })
open(url, callbackFn)

Opens a webpage with url and callback function arguments.

require('phantom-proxy').createProxy({}, function(proxy){
  var page = proxy.page,
  phantom = proxy.phantom;
  
  page.open('http://www.w3.org', function(){
    console.log('page now open');
  });  
  
});
waitForSelector(selector, callbackFn)

Polls page for presence of selector, executes callback when selector is present.

require('phantom-proxy').createProxy({}, 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');
    });
    console.log('page now open');
  });  
  
});
render(fileName, callbackFn)

Renders a image of browser.

require('phantom-proxy').createProxy({}, 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!');
      });
    });
    console.log('page now open');
  });  
  
});
renderBase64(type, callbackFn)

Returns a base64 representation of image.

evaluate(functionToEvaluate, callbackFn, [arg1, arg2,... argN]

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.

Events

Subscribing to events

phantomProxy = require('phantom-proxy');
phantomProxy.createProxy({}, function (proxy) {
    proxy.page.onConsoleMessage = function (event) {
        console.log(JSON.stringify(event));
    };
});

FAQ

Why do we need another nodejs runtime for phantom?

Phantom-proxy takes a different approach to communicating with phantom than these modules. Phantom-node passes messages using alerts and express. While this was an ingenious solution to a difficult problem at the time, better solutions are now available. As of version 1.4, phantomjs provides an embedded webserver called mongoose which can be used to pass messages. Phantom-proxy leverages this, which is the fastst and most reliable approach.

Current Tags

  • 0.1.792                                ...           latest (14 years ago)

42 Versions

  • 0.1.792                                ...           14 years ago
  • 0.1.791                                ...           14 years ago
  • 0.1.79                                ...           14 years ago
  • 0.1.78                                ...           14 years ago
  • 0.1.77                                ...           14 years ago
  • 0.1.76                                ...           14 years ago
  • 0.1.75                                ...           14 years ago
  • 0.1.74                                ...           14 years ago
  • 0.1.73                                ...           14 years ago
  • 0.1.721                                ...           14 years ago
  • 0.1.72                                ...           14 years ago
  • 0.1.71                                ...           14 years ago
  • 0.1.69                                ...           14 years ago
  • 0.1.68                                ...           14 years ago
  • 0.1.67                                ...           14 years ago
  • 0.1.66                                ...           14 years ago
  • 0.1.65                                ...           14 years ago
  • 0.1.64                                ...           14 years ago
  • 0.1.62                                ...           14 years ago
  • 0.1.61                                ...           14 years ago
  • 0.1.51                                ...           14 years ago
  • 0.1.2                                ...           14 years ago
  • 0.1.19                                ...           14 years ago
  • 0.1.18                                ...           14 years ago
  • 0.1.17                                ...           14 years ago
  • 0.1.16                                ...           14 years ago
  • 0.1.15                                ...           14 years ago
  • 0.1.14                                ...           14 years ago
  • 0.1.12                                ...           14 years ago
  • 0.1.10                                ...           14 years ago
  • 0.1.9                                ...           14 years ago
  • 0.1.8                                ...           14 years ago
  • 0.1.7                                ...           14 years ago
  • 0.1.6                                ...           14 years ago
  • 0.1.5                                ...           14 years ago
  • 0.1.4                                ...           14 years ago
  • 0.1.3                                ...           14 years ago
  • 0.1.1                                ...           14 years ago
  • 0.1.0                                ...           14 years ago
  • 0.0.3                                ...           14 years ago
  • 0.0.2                                ...           14 years ago
  • 0.0.1                                ...           14 years ago
Maintainers (1)
Downloads
Total 117
Today 0
This Week 0
This Month 55
Last Day 0
Last Week 0
Last Month 62
Dependencies (5)
Dev Dependencies (3)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE