Skip to content

Instantly share code, notes, and snippets.

@jimkang
Created January 3, 2014 20:21
Show Gist options
  • Select an option

  • Save jimkang/8245790 to your computer and use it in GitHub Desktop.

Select an option

Save jimkang/8245790 to your computer and use it in GitHub Desktop.
A really basic Phantom.js test.
// Run with phantomjs test.js http://whatever.com
// var chai = require('./node_modules/chai/index.js');
var page = require('webpage').create();
var system = require('system');
if (system.args.length === 1) {
console.log('Usage: test.js <some URL>');
phantom.exit();
}
var assert = function assert(condition, message) {
if (!condition) {
console.log('Assertion failed:', message);
}
};
var t = Date.now();
var address = system.args[1];
page.open(address, function onPageOpened(status) {
if (status !== 'success') {
console.log('FAIL to load the address');
}
else {
t = Date.now() - t;
console.log('Loading time ' + t + ' msec');
frontPageSuite(page);
}
phantom.exit();
});
function frontPageSuite(page) {
var loginLink = page.evaluate(function findLoginLink() {
return document.querySelector('a[href="/something/login"]');
});
assert(loginLink, 'Login link exists.');
page.onLoadFinished = function(status){
console.log('hey');
}
page.sendEvent('click', loginLink.offsetLeft + 20, loginLink.offsetTop + 20);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment