Navigation Menu

Skip to content

Commit

Permalink
phantomjs stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Trostler committed Nov 9, 2011
1 parent f66deea commit b9c285a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 120 deletions.
114 changes: 0 additions & 114 deletions backend/nodejute/jute/actions/phantomJS.js

This file was deleted.

21 changes: 15 additions & 6 deletions backend/nodejute/jute/actions/startPhantomjs.js
Expand Up @@ -47,20 +47,26 @@ module.exports = {
// Events I care about
hub.addListener('action:phantomjsStart', startPhantomjs);

function Phantomjs(selID, phantomjs, screen, req, res) {
function startPhantomjs(selID, phantomjs, screen, req, res) {
var cb, phantom, body = req.body,
url = 'http://' + (hub.config.host ? hub.config.host + ':' + hub.config.port : req.headers.host) + '/?selenium=' + selID
;

try {
phantom = child.spawn(phantomjs, [url]);
hub.emit(hub.LOG, hub.INFO, "DISPLAY=:" + screen + ' ' + phantomjs + ' ' + path.join(__dirname, '..', "phantomJUTE.js") + ' ' + url);
process.env.DISPLAY = ':' + screen;
phantom = child.spawn(phantomjs, [ path.join(__dirname, '..', "phantomJUTE.js"), url]);
phantom.stdout.on('data', function(data) {
hub.emit(hub.LOG, hub.INFO, "PhantomJS up and running: " + data);
hub.emit(hub.LOG, hub.INFO, "PhantomJS sez: " + data);
});
phantom.stderr.on('data', function(data) {
hub.emit(hub.LOG, hub.INFO, "PhantomJS stderr: " + data);
});
phantom.on('exit', function() {
hub.emit(hub.LOG, hub.INFO, "PhantomJS up done");
if (!phantom.done) {
hub.emit(hub.LOG, hub.ERROR, "PhantomJS exited unexpectedly");
cb('PhantomJS executable exited unexpectedly');
}
});

} catch(e) {
Expand All @@ -69,9 +75,9 @@ module.exports = {
}

// Give Selenium 1000 minutes to finish - should be good - 16 hours baby!
req.socket.setTimeout(60000000, function() {
req.socket.setTimeout(6000000, function() {
hub.emit(hub.LOG, hub.ERROR, 'Phantomjs taking too long - giving up');
cb();
cb('took too long!');
});

cache.connections[selID] = res; // our link back to the requesting client for status messages
Expand All @@ -80,6 +86,9 @@ module.exports = {
// && keep track of requesting client for debug messages back...
// Callback for when the phantomjs process is done
cb = function(err) {
hub.emit(hub.LOG, hub.INFO, 'Phantomjs done!');
phantom.done = true;
phantom.kill()
delete cache.connections[selID]; // done with status updates
hub.emit('action:phantomjsDone', err, selID);
};
Expand Down
53 changes: 53 additions & 0 deletions backend/nodejute/jute/phantomJUTE.js
@@ -0,0 +1,53 @@
/**
* Wait until the test condition is true or a timeout occurs. Useful for waiting
* on a server response or for a ui change (fadeIn, etc.) to occur.
*
* @param testFx javascript condition that evaluates to a boolean,
* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
* as a callback function.
* @param onReady what to do when testFx condition is fulfilled,
* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
* as a callback function.
* @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
*/
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000000, //< Default Max Timout is 3s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log("'waitFor()' timeout");
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}, 1000); //< repeat check every 1000ms
};


var page = new WebPage();

page.onAlert = function(msg) { console.log("ALERT: " + msg); };
page.onConsoleMessage = function (msg) { console.log("CONSOLE: " + msg); };
#page.onResourceRequested = function (msg) { console.log("Request: " + msg.url); };
#page.onResourceReceived = function (msg) { console.log("Received: " + msg.url); };

// Open Twitter on 'sencha' profile and, onPageLoad, do...
page.open(phantom.args[0], function (status) {
// Check for page load success
if (status !== "success") {
console.log("Unable to access network");
} else {
waitFor(function(){}, function(){});
}
});

0 comments on commit b9c285a

Please sign in to comment.