Git Product home page Git Product logo

revenant's Introduction

Revenant

CI Build Status npm version

A headless browser powered by PhantomJS functions in Node.js. Based on the PhantomJS-Node bridge.

This library aims to abstract many of the simple functions one would use while testing or scraping a web page. Instead of running page.evaluate(...) and entering the javascript functions for a task, these tasks are abstracted for the user.

For example, a robust syntax to click an element on a page that has to be executed in the browser javascript environment is:

 var button = document.querySelector(SELECTOR);
 var ev = document.createEvent("MouseEvent");
 ev.initMouseEvent(
     "click",
     true /* bubble */, true /* cancelable */,
     window, null,
     0, 0, 0, 0, /* coordinates */
     false, false, false, false, /* modifier keys */
     0 /*left*/, null
 );
 button.dispatchEvent(ev);

With Revenant, the equivalent would be:

revenant.clickElement(SELECTOR, 0, callback);

When to execute the callback can also be configured, if we desire to wait for some ajax element to appear. See the API documentation below for more details.

Contents

Installation

Make sure PhantomJS is installed in your PATH, and run

$ npm install --save revenant

Usage

Example code to open a browser, wait for an element to appear on the page, and then logs its innerHTML to the console.

var Revenant = require('revenant');

// example AJAX test page
const URL = 'http://jiahaog.github.io/ajax-test-page/';

// selector for AJAX content
const SELECTOR = '#setTimeoutContent';

// create a browser
var browser = new Revenant();

browser
    .openPage(URL)
    .then(function () {
        return browser.waitForElement(SELECTOR);
    })
    .then(function () {
        return browser.getInnerHTML(SELECTOR);
    })
    .then(function (result) {
        console.log(result); // 'BUBBLES'

        // kills the PhantomJS process
        browser.done();

    }).catch(function (error) {
        browser.done();
    });

All API functions return a promise, and if desired, they can be used to chain callbacks. Alternatively, the conventional node callback can be provided as the last argument in these functions.

API

Initialisation

Revenant is the main object to simulate a browser. Initialise an instance with new, and call done() to kill the PhantomJS process when the task is completed.

Example

var browser = new Revenant();

browser
	.openPage(URL)
	
	.then(//... do things )
	
	.then(function (result) {
		browser.done();
	})
	.catch(function (error) {
		browser.done();
	});

openPage(url, [callback])

Opens a url within PhantomJS. Always call this function first to initialise the browser and open a page, before continuing with other tasks.

Arguments

  • urlstring Url to open
  • callback(error)Optional function Callback called when the page has been opened

getUrl([callback])

Gets the current url of the browser.

Arguments

  • callback(error, url)Optional function Callback called when the url has been retrieved

navigateToUrl(url, [callback])

Navigates the current page to another url.

Arguments

  • callback(error)Optional function Callback called when a new page is loaded and the DOM is ready

takeSnapshot([callback])

Takes a snapshot of the DOM into a string.

Arguments

  • callback(error, dom)Optional function Callback called when a new page is loaded and the DOM is ready. dom will be a snapshot of the entire document as a string

waitForElement(selector, [callback])

Polls the page and waits for a particular CSS selector to appear.

Arguments

  • selectorstring CSS selector to choose the element to wait for
  • callback(error)Optional function Callback called when that css selector is now present on the page

waitForDomString(stringQuery, [callback])

Like waitForElement(), except that it waits for a particular string to appear in the DOM before executing the callback.

Arguments

  • stringQuerystring To query the DOM for
  • callback(error)Optional function Callback called when the stringQuery is found contained in the DOM.

getInnerHTML(selector, [callback])

Polls the page and waits for a particular CSS selector to appear, and then gets the innerHTML of that element .

Arguments

  • selectorstring CSS selector to choose the element to wait for
  • callback(error, innerHtml)Optional function Callback called when that css selector is now present on the page

changeDropdownIndex(selector, value, [callback])

Changes the selected index of a dropdown element.

Arguments

  • selectorstring CSS selector to choose the dropdown element
  • valueinteger index of the dropdown to switch to
  • callback(error)Optional function Callback called when the dropdown index has been changed

clickElement(selector, options, [callback])

Clicks a element on the page

Arguments

  • selectorstring CSS selector to choose the element to click
  • optionsinteger Options to affect when to execute the callback.
    • 0 – Execute callback immediately after clicking
    • 1 – Expect an ajax change in the page, only execute callback when the DOM has changed by a certain threshold
    • 2 – Expect page navigation, so execute the callback only when the url changes and the new document is ready
  • callback(error)Optional function Callback called when the criteria set in options has been met

setCheckboxState(selector, state, [callback])

Sets the state of a checkbox. Also fires an event to simulate a mouse click of the checkbox.

Arguments

  • selectorstring CSS selector to choose the checkbox element
  • stateboolean Value to indicate if the checkbox should be checked
  • callback(error)Optional function Callback called when the dropdown index has been changed

fillForm(selector, value, [callback])

Fills a form on the page.

Arguments

  • selectorstring CSS selector to choose the form element
  • valuestring Value to fill the form
  • callback(error)Optional function Callback called when the form has been filled

submitForm([callback])

Submits the form on the page.

Arguments

  • callback(error)Optional function Callback called when the form has been submitted, the page has reached a new url, and the document is ready

downloadFromUrl(url, [callback])

Downloads a file at a URL, using the current cookies as credentials for the http request.

Arguments

  • url - string URL to the desired file
  • callback(error, file)Optional function Callback called after the file has been downloaded. file is the retrieved data in bytes

downloadFromClick(selector, [callback])

Clicks an element on the page and downloads the file behind the element

Arguments

  • selector - string Selector for the element
  • callback(error, file)Optional function Callback called after the file has been downloaded. file is the retrieved data in bytes

Test

Tests are done with Mocha.

$ npm test

A test web page with asynchronous content to simulate a web app was created for testing purposes, and can be found at http://jiahaog.github.io/ajax-test-page/.

revenant's People

Contributors

jiahaog avatar naholyr avatar

Stargazers

Andrew Johnson avatar Lewis Dimmick  avatar Owen Dall Sotomayor avatar  avatar karim avatar Matthias avatar Priyansh Jain avatar GAURAV avatar Impetus avatar David Danziger avatar Rakesh Reddy avatar Yevchenko Stanislav avatar rumblefrog avatar  avatar Sonny Lazuardi avatar Brian avatar Atef Ben Ali avatar Kyle Oliveiro avatar Roman Murashov avatar Lawrence Dark avatar Andre Tessmann avatar Huei Tan avatar Moritz Göckel avatar Dale Inverarity avatar Vinicius Diogo de Melo avatar Eirik L. Vullum avatar Lyle Pratt avatar ROXY CHEN avatar Nitin Bansal avatar Starck Lin avatar Anton Dvorson avatar Rodolfo Wilhelmy avatar Lev Brie avatar LogikIO avatar Boris Köster avatar Rich Young avatar Sam Gluck avatar Serge R avatar Alexey Komarov avatar Kyros Koh avatar  avatar Mike James avatar Shubham Goyal avatar Radu Topala avatar Paul S. Mash avatar Kumarajiva avatar Will Sahatdjian avatar Christian Hochfilzer avatar LC avatar walterzhang avatar Piotr Kubisa avatar Parin avatar Gabor Dolla avatar wei.liu avatar Adrien Becchis avatar Xiaofan Hu avatar  avatar Boris Cherny avatar noa avatar Chris Bernal avatar Fabien Bourgeois avatar Alan James avatar Lau Siaw Young avatar Mike Metzger avatar WANG QIANG avatar Fabien Franzen avatar Dave avatar Gregory Scheerlinck avatar Demián Rodriguez avatar AB avatar Mr.Q avatar Frank avatar Damian Mullins avatar Porramate Lim avatar Michael Hernandez avatar Sergio Araki avatar Jodoo Shi avatar Peh Qin Cheng avatar Simon avatar 斯人 avatar Kevin Dente avatar Seoh Char avatar Vincent Reydet avatar Zero avatar Paul Day avatar Alessio avatar Kevin Isom avatar Max Maximov avatar Bertrand CHEVRIER avatar Sean Goresht avatar Justin Sisley avatar  avatar John Goodall avatar Wil Tan avatar praveenvignesh avatar Sean Vieira avatar Jan Florian Dietrich avatar Leonardo Iglesias avatar Jan Vlnas avatar Mio Green avatar

Watchers

ND avatar Christian Hochfilzer avatar James Cloos avatar  avatar Walter Di Carlo avatar  avatar Bill Christo avatar  avatar

revenant's Issues

iframe support?

I'd be interested in using this over casper.js but I need a few things to work in side of iframes? Is that possible / planned?

File download support if possible

Unfortunately just using openPage doesn't allow Revenant to download files, the URL simply does not change and onResourceReceived fails to get anything in my case. This could be resolved (I have not tested it) by the answers here

Broken with PhantomJS 2.1.1

The examples don't work on PhantomJS 2.1.1, but they do on 2.0.0. The program just hangs without executing anything.

evaluate?

I would like to manipulate the DOM and I cannot find an evaluate method. In fact, many useful methods are not available. Are they hidden somewhere?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.