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.
Phantom.js + jQuery = AwesomeSauce!
Node.js +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 $ = ; // Visit the user path and log in.$;
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 $ = ; // Visit the user path and log in.$;
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 $ = ; // Visit the user path and log in.$;
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 $ = ; // Add some default configs.$configsite = 'http://www.whitehouse.gov';$configaddJQuery = false; // Go to the presidents page.$;
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.
$;
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.$;
-
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.
$;
-
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 $ = ;$;
-
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.9.1.min.js') - The CDN url of the jQuery to add to the page if addJQuery is set to TRUE.
var $ = ; // Add some default configs.$configsite = 'http://localhost:8888';$configaddJQuery = false; // Visit the user path and log in.$;
Async.js
jQuery.go - Using this library withThis 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 = ;var $ = ; // Add some default configs.$configsite = 'http://localhost:8888';$configaddJQuery = false; // Using the async.series with jQuery.go.async;
This makes it so that you do not fall into Callback hell when developing your automation and tests.