• Stars
    star
    150
  • Rank 242,386 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 11 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An easy-to-use web testing and automation tool that uses the jQuery interface within Node.js to interact with the Phantom.js browser.

THIS MODULE IS NO LONGER BEING MAINTAINED. I would recommend using something like Casper.js instead.

jquery.go.js

An easy-to-use web testing and automation tool that uses the jQuery interface within Node.js to interact with the Phantom.js browser.

Node.js + Phantom.js + jQuery = AwesomeSauce!

What do you get when you combine three of the most exciting JavaScript technologies into a single package. AwesomeSauce that's what. Actually, what you get is an easy to use browser testing and automation tool by utilizing the jQuery interface within Node.js to interact with the Phantom.js browser.

Another jQuery wrapper in Node.js?...

Yes... but not really... Obviously, there are other technologies that wrap the jQuery library within a Node.js environment, but this library is different.

For one, this library is not a complete API mirror of jQuery. Every API is asynchronous (due to its interaction with Phantom.js), so there are some differences. Because of this, I would rather think of this library as a tool for testing and automation, but just uses the familar jQuery API to do so. Technically speaking, it accomplishes this by simply passing along your commands to jQuery within Phantom.js, but there are also some other methods exposed to help with the task of testing and automation.

In particular is a method called jquery.go that allows this library to be used with the Async.js library.

Installation

  • Obviously you need to install Node.js to use this.
  • You can now use this library using the NPM package jquerygo
npm install jquerygo

The API

Every thing within jQuery (for the most part) is exposed to this library, but done so asynchronously. For example, to get the text of an element on the page, you would use the following code.

var $ = require('jquerygo');

// Visit the user path and log in.
$.visit('http://localhost:8888/user', function() {

  // Get the title.
  $('h1').text(function(text) {
    console.log(text);
    $.close();
  });
});

To set the text of an element on the page, you still add your arguments after text, but just include the callback to know when it is done..

var $ = require('jquerygo');

// Visit the user path and log in.
$.visit('http://localhost:8888/user', function() {

  // Set the title.
  $('h1').text('New Title', function() {

    // Get the title that was set.
    $('h1').text(function(text) {

      // Should print 'New Title'.
      console.log(text);
      $.close();
    });
  });
});

You may have noted that this makes jQuery chaining difficult. You are right, but you don't need to repeat your jQuery selectors since if you wish to chain, you can just use the 'this' keyword in the callbacks to reference the same selector.

var $ = require('jquerygo');

// Visit the user path and log in.
$.visit('http://localhost:8888/user', function() {

  // Set the title.
  $('h1').text('New Title', function() {

    // Use 'this' to get the title of the same selector
    this.text(function(text) {

      // Should print 'New Title'.
      console.log(text);
      $.close();
    });
  });
});

The each method

This library also supports the each method, but its signature is a little bit different than core jQuery. The main difference being that it must support asynchronous process flow using callback functions. Here is an example of using the each method.

var $ = require('jquerygo');

// Add some default configs.
$.config.site = 'http://www.whitehouse.gov';
$.config.addJQuery = false;

// Go to the presidents page.
$.visit('/about/presidents', function() {
  $.waitForPage(function() {
  
    // Iterate through each span.field-content.
    $('span.field-content').each(function(index, element, done) {
    
      // Get the text of this element.
      element.text(function(name) {
      
        // Print the presidents name.
        console.log(name);
        done();
      });
    }, function() {
    
      // We are done.
      console.log('Presidents loaded!');
      $.close();
    });
  });
});

That is pretty much what you need to know regarding differences between jQuery interface compared to what you are used to.

Screen Capture

This library also allows you to take a screen capture during your testing and automation. This can be done using the $.capture method. You can also use this along with __dirname to take a screen shot within the directory that your automation script resides.

$.capture(__dirname + '/screenshot.png');

Additional API's

There are also some added API's that make your testing and automation easier. They are as follows.

  • visit: function(url, callback)

    Visit a webpage.

    • url: The url you wish to visit.
    • callback: Called when you are done visiting that page.
// Visit the user path and log in.
$.visit('http://localhost;8888', function() {
  console.log('You have visited the page!');
  $.close();
});
  • waitForPage: function(callback)

    Wait for a page to load. Useful after you press Submit on a form.

    • callback: Called when the page is done loading.
$.visit('/user', function() {
  $('#edit-name').val('admin', function() {
    $('#edit-pass').val('123password', function() {
      $('#edit-submit').click(function() {
        $.waitForPage(function() {
          console.log('You have logged in!');
          $.close();
        });
      });
    });
  });
});
  • waitForElement: function(element, callback)

    Wait for an element to appear on the page. Useful when you are waiting for an AJAX request to return which sets an element on the page.

    • element: The element selector that you wish to wait for.
    • callback: Called when that element is present.
  • getPage: function(callback)

    Return the phantomJS page so that you can perform your own JS execute parameter.

    • callback: Called when the page is returned.
var $ = require('../lib/jquery.go.js');
$.visit('https://www.google.com', function(){
  $.waitForPage(function(){
    $.getPage(function(page) {
      page.evaluate(function(args) {
      
        // Just return the passed in params.
        return args.hello;
      }, function(err, retVal) {
      
        // Called when you return from the evaluation.
        console.log(retVal);
        $.close();
      }, {
        hello: 'HELLO THERE!!!'
      });
    });
  });
});
  • close: function()

    Closes the Phantom.js browser.

  • config: object

    An object of configurations for this library.

    • site: (string, default='') - A base url for the site so that all other 'visit' calls could be relative.
    • addJQuery: (boolean, default=TRUE) - TRUE if you need to add jQuery to the page you are visiting, FALSE if the page already adds jQuery.
    • jQuery: (string, default='http://code.jquery.com/jquery-1.11.1.min.js') - The CDN url of the jQuery to add to the page if addJQuery is set to TRUE.
    • width: (int, default=1920) - The width of the viewport
    • height: (int, default=1080) - The height of the viewport
    • userAgent: (string, defaults to phantoms agent) - Set a custom user agent
var $ = require('jquerygo');

// Add some default configs.
$.config.site = 'http://localhost:8888';
$.config.addJQuery = false;

// Visit the user path and log in.
$.visit('/user', function() {
  $('#edit-name').val('admin', function() {
    $('#edit-pass').val('123password', function() {
      $('#edit-submit').click(function() {
        $.waitForPage(function() {
          console.log('You are logged in!');
        });
      });
    });
  });
});

jQuery.go - Using this library with Async.js

This library is called jQuery.go for a reason. It is because there is a special method that is used to interact with the Async.js library that provides an easy way to provide a serial looking interface when building your tests. This can work with the Async.js library by calling the go method and whatever functions you wish to call after that as arguments to that method.

A great example is to take the previous example shown above and rewrite it using jQuery.go method.

var async = require('async');
var $ = require('../lib/jquery.go.js');

// Add some default configs.
$.config.site = 'http://localhost:8888';
$.config.addJQuery = false;

// Using the async.series with jQuery.go.
async.series([
  $.go('visit', '/user'),
  $('#edit-name').go('val', 'admin'),
  $('#edit-pass').go('val', '123testing'),
  $('#edit-submit').go('click'),
  $.go('waitForPage'),
  function(done) {

    // Make sure that the logout link is shown.
    $('a[href="/user/logout"]').text(function(text) {
      console.log(text);
      done();
    });
  }
], function() {
  console.log('You are logged in!');
  $.close();
});

This makes it so that you do not fall into Callback hell when developing your automation and tests.

More Repositories

1

jsencrypt

A zero-dependency Javascript library to perform OpenSSL RSA Encryption, Decryption, and Key Generation.
JavaScript
6,537
star
2

makemeasandwich.js

A Node.js + Phantom.js command line application that will automatically order you a sandwich from Jimmy John's. ( http://xkcd.com/149 )
JavaScript
963
star
3

seamless.js

A Javascript library for working with seamless iframes.
JavaScript
215
star
4

meanapp

An example M.E.A.N web application with full CRUD features.
ApacheConf
162
star
5

resourcejs

An minimalistic Express.js library that will reflect a Mongoose model onto a RESTful interface with a splash of Swagger.io love.
JavaScript
115
star
6

jquery.treeselect.js

A minimalistic jQuery hierarchy select widget used for selecting hierarchy structures in a treeview format.
JavaScript
101
star
7

drupal.api.js

An object oriented JavaScript API Library for RESTful Drupal CMS.
JavaScript
55
star
8

zombie-phantom

Provides a Zombie.js shim around the Phantom.js Headless Browser.
JavaScript
51
star
9

presentations

A list of all my presentations.
JavaScript
48
star
10

minplayer

A minimalistic, plugin-based "core" media player for the web.
JavaScript
24
star
11

dartminer

A Bitcoin miner written in the Dart language.
Dart
20
star
12

drupal.go.js

A node.js package to automate and test Drupal using the Phantom.js headless browser.
JavaScript
16
star
13

mediafront_demo

A demo Drupal 7 site to show off the mediafront module.
12
star
14

zerotomean

The example application for 0 to M.E.A.N presentation
JavaScript
10
star
15

flatiron-passport

A Passport.js integration with the Flatiron.js web framework.
JavaScript
10
star
16

groupselfie

A web application that allows you to take Group Selfies
JavaScript
9
star
17

drupaltouch

A Sencha Touch application for Drupal CMS
JavaScript
8
star
18

drupal_multimedia

A Drupal 8 with multimedia support.
PHP
7
star
19

youtube_playlist

This is an example jQuery Mobile widget for showing YouTube Playlists. Go to http://www.youtube.com/watch?v=RlrJthCmmU8 to watch the presentation.
JavaScript
6
star
20

ResultTree

A PHP class that will take a flat parent-child mapping array, and build a result tree non-recursively.
PHP
6
star
21

limelight

A PHP library for integrating with Limelight CDN
PHP
5
star
22

restPHP

A couple of simple helper classes to help in writing RESTful PHP Libraries.
PHP
5
star
23

pivotalphp

This is a GPLv3 PHP-CLI script for Pivotal Tracker
PHP
5
star
24

formiomean

An example M.E.A.N app using Angular 4 + Form.io
TypeScript
5
star
25

juckstrap

Unfortunately named static site generation utilizing Nunjucks + Bootstrap + Wintersmith.js.
JavaScript
4
star
26

cliphp

An easy to use CLI ( command line interface ) PHP class that allows for user input.
PHP
4
star
27

phptoolbox

The PHP Toolbox is a convenient way to execute and manage your PHP scripts.
PHP
3
star
28

jekyll-kickstart

A starter Jekyll site using BootStrap 3
JavaScript
3
star
29

travist.github.com

My github pages site.
HTML
3
star
30

media_feature

A Drupal 7 media feature module.
PHP
3
star
31

CachedRequest

An extension to PEAR's HTTP_Request2 class that implements extensible caching mechanisms.
PHP
3
star
32

ropemaze

The rope maze puzzle solved using brute force.
JavaScript
3
star
33

minplayer-flash

An MIT licensed, minimalistic, skinnable, plugin based Flash media player
ActionScript
3
star
34

clicpp

This is a helper class to assist with creating command line applications in C++ where you can easily gather settings for your application.
C++
2
star
35

rotating_banner_pan

A plugin for Drupal's Rotating Banner module that implements panning.
JavaScript
2
star
36

CCK

Content Construction Kit Clone
PHP
2
star
37

drupal-zombie

A presentation and examples for "Automating and Testing Drupal with Zombie.js"
JavaScript
2
star
38

pdfjs-viewer

A built bower version of PDFJS
JavaScript
2
star
39

fpManager

A plugin manager for Flash applications.
ActionScript
2
star
40

moviemate

A Chrome extension that merges functionality between YouTube Trailers and IMDB.
JavaScript
2
star
41

jquery.moreorless.js

A quick way to add "more" or "less" functionality to HTML elements using jQuery.
JavaScript
2
star
42

JQImage.js

A jQuery widget and wrapper class for working with Images.
JavaScript
2
star
43

limelight.js

A node.js application for authenticating Limelight API requests.
JavaScript
1
star
44

drupal-media-module

This is the Drupal Media module with media player integration.
JavaScript
1
star
45

generator-juckstrap

A Yeoman generator for juckstrap.
JavaScript
1
star
46

async-shell

A super simple async shell for Node.js
JavaScript
1
star