-
-
Save mbejda/fac59b9e43ea20f27351 to your computer and use it in GitHub Desktop.
PhantomJS Async Steps Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var page = new WebPage(), testindex = 0, loadInProgress = false; | |
page.onConsoleMessage = function(msg) { | |
console.log(msg); | |
}; | |
//This sets loadInProgress to true when a form is submitted and the page gets redirected. | |
page.onNavigationRequested = function() { | |
loadInProgress = true; | |
} | |
page.onLoadStarted = function() { | |
loadInProgress = true; | |
console.log("load started"); | |
}; | |
page.onLoadFinished = function() { | |
loadInProgress = false; | |
console.log("load finished"); | |
}; | |
var steps = [ | |
function() { | |
//Load Login Page | |
page.open("https://website.com/theformpage/"); | |
}, | |
function() { | |
//Enter Credentials | |
page.evaluate(function() { | |
var arr = document.getElementsByClassName("login-form"); | |
var i; | |
for (i=0; i < arr.length; i++) { | |
if (arr[i].getAttribute('method') == "POST") { | |
arr[i].elements["email"].value="mylogin"; | |
arr[i].elements["password"].value="mypassword"; | |
return; | |
} | |
} | |
}); | |
}, | |
function() { | |
//Login | |
page.evaluate(function() { | |
var arr = document.getElementsByClassName("login-form"); | |
var i; | |
for (i=0; i < arr.length; i++) { | |
if (arr[i].getAttribute('method') == "POST") { | |
arr[i].submit(); | |
return; | |
} | |
} | |
}); | |
}, | |
function() { | |
// Output content of page to stdout after form has been submitted | |
page.evaluate(function() { | |
console.log(document.querySelectorAll('html')[0].outerHTML); | |
}); | |
} | |
]; | |
interval = setInterval(function() { | |
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment