Skip to content

Instantly share code, notes, and snippets.

@Lax
Created August 4, 2017 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lax/7986033b459b70302447e98fb02d109a to your computer and use it in GitHub Desktop.
Save Lax/7986033b459b70302447e98fb02d109a to your computer and use it in GitHub Desktop.
"use strict";
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(),
system = require('system'),
t, address;
page.onConsoleMessage = function(msg) {
// console.log(msg);
};
if (system.args.length === 1) {
console.log('Usage: loadspeed.js <some URL>');
phantom.exit();
}
t = Date.now();
address = system.args[1];
page.open(address, function(status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
console.log('Loading ' + system.args[1]);
console.log('Loading time ' + t + ' msec');
var title = page.evaluate(function() {
return document.title;
});
console.log('Page title is ' + title);
waitFor(function(){
return page.evaluate(function() {
// return typeof(document.getElementById('J_PromoPriceNum')) != null;
return document.getElementById('J_PromoPriceNum')
});
}, function() {
var promo_price = page.evaluate(function() {
return document.getElementById('J_PromoPriceNum').textContent;
})
var price = page.evaluate(function() {
return document.getElementById('J_StrPrice').getElementsByClassName('tb-rmb-num')[0].textContent;
})
console.log('Price is ' + price);
console.log('Promo Price is ' + promo_price);
phantom.exit(1);
}, 10000);
}
// phantom.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment