【发布时间】:2015-05-19 20:52:08
【问题描述】:
我可以使用示例中提供的 waitfor 函数 https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js 我也可以使用这里提供的 includeJs http://phantomjs.org/page-automation.html
我很难弄清楚如何将 jquery 包含在 waitfor 函数中。
下面的 sudo 代码只是我尝试过的众多方法之一。请帮忙。
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();
page.open("http://example.com", function (status) {
if (status !== "success") {
console.log("Unable to access network");
} else {
waitFor(function() {
// This does not work
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
return page.evaluate(function() {
return $("#some-element").is(":visible");
});
});
}, function() {
console.log("some-element should be visible now.");
phantom.exit();
});
}
});
【问题讨论】:
标签: javascript jquery phantomjs