Skip to content

Instantly share code, notes, and snippets.

@retrozoid
Last active August 29, 2015 14:27
Show Gist options
  • Save retrozoid/fc24dbfd9fe9b5f236fe to your computer and use it in GitHub Desktop.
Save retrozoid/fc24dbfd9fe9b5f236fe to your computer and use it in GitHub Desktop.
Just a way to work with pages synchronously based on waitFor events
var page = require('webpage').create();
page.viewportSize = {
width: 1366,
height: 768
};
var waitFor = function(testFx, onReady, onTimeout, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 5000,
start = new Date().getTime(),
condition = false,
wInterval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx());
} else {
if (condition) {
console.log("wait() finished in " + (new Date().getTime() - start) + "ms.");
typeof(onReady) === "string" ? eval(onReady) : onReady();
} else {
typeof(onTimeout) === "string" ? eval(onTimeout) : onTimeout();
}
clearInterval(wInterval);
}
}, 250);
};
var wait = function(selector, ready) {
waitFor(
'page.evaluate(function() {' +
' var a = document.querySelector("' + selector + '");' +
' return a == null ? false : window.getComputedStyle(a).visibility === "visible";' +
'});',
function() {
setTimeout(ready, 3000);
},
function() {
page.render("timeout.png");
sendAlert("Error: wait('" + selector + "') timeout. See screenshot timeout.png");
}, 30000);
};
page.open("https://my.ecwid.com/cp", function (status) {
wait("form[action='/cp/login'] input[name='email']", function() {
page.evaluate(function() {
document.querySelector("form[action='/cp/login'] input[name='email']").value = "login";
document.querySelector("form[action='/cp/login'] input[name='password']").value = "pass";
document.querySelector("form[action='/cp/login'] button[type='button']").click();
});
wait(".OnboardingWizard-progress", function() {
phantom.exit(0);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment