Skip to content

Instantly share code, notes, and snippets.

@carlcrott
Created April 19, 2014 00:02
Show Gist options
  • Save carlcrott/11069102 to your computer and use it in GitHub Desktop.
Save carlcrott/11069102 to your computer and use it in GitHub Desktop.
lilith@lilith:~/javascript_projects/screengrab$ phantomjs login.js
step 1
load started
loading app.js
loading routes.js
loading config.js
loading services.js
loading service.login.js
loading service.firebase.js
loading service.events.js
loading module.waitForAuth.js
loading module.routeSecurity.js
loading filters.js
loading index.js
No phonegap...
Application starting...
CREATING THE ROUTE SECURITY MANAGER
setting up firebase loginService from app.js.run()
init called in loginService
servicelogin:--------INIT
setting up $rootScope.back() from app.js.run()
Starting notification callback...
Can't use local plugins without phonegap...
loading IndexCtrl...
loading LoginCtrl...
load finished
step 2
step 3
Login clicked
<html><head></head><body></body></html>
step 4
test complete!
lilith@lilith:~/javascript_projects/screengrab$
var page = new WebPage(), testindex = 0, loadInProgress = false;
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
var steps = [
//Load Login Page
function() {
page.open("http://localhost/www/#/login");
},
//Enter Credentials
function() {
page.evaluate(function() {
var email = document.getElementById("email_input");
var password = document.getElementById("password_input");
email.value="[email protected]";
password.value="a";
});
},
//Login
function() {
var a = page.evaluate(function() {
return document.querySelector('#login-btn');
});
page.sendEvent('click', a.offsetLeft, a.offsetTop);
console.log("Login clicked");
console.log(document.querySelectorAll('html')[0].outerHTML);
},
// Output content of page to stdout after form has been submitted
function(status) {
// Wait for 'signin-dropdown' to be visible
waitFor(function() {
// Check in the page if a specific element is now visible
return page.evaluate(function() {
return $(".lb-top-title").is(":visible");
});
}, function() {
console.log(document.querySelectorAll('html')[0].outerHTML);
page.render('ladybug.png');
});
}
];
interval = setInterval(function(page) {
if (!loadInProgress && typeof steps[testindex] == "function") {
console.log("step " + (testindex + 1));
steps[testindex]();
testindex++;
}
if (typeof steps[testindex] != "function") {
console.log("test complete!");
phantom.exit();
}
}, 50);
//////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 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 : 3000, //< 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
}
}
}, 250); //< repeat check every 250ms
};
//var page = require('webpage').create();
// Open Twitter on 'sencha' profile and, onPageLoad, do...
//page.open("http://twitter.com/#!/sencha", function (status) {
// // Check for page load success
// if (status !== "success") {
// console.log("Unable to access network");
// } else {
// // Wait for 'signin-dropdown' to be visible
waitFor(function() {
// Check in the page if a specific element is now visible
return page.evaluate(function() {
return $("#signin-dropdown").is(":visible");
});
}, function() {
console.log("The sign-in dialog should be visible now.");
phantom.exit();
});
// }
//});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment