Phantomjs 将 waitfor 与 includeJs 集成
Phantomjs integrating waitfor with includeJs
我能够使用示例中提供的 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();
});
}
});
如您在 waitFor()
代码中所见,testFx
函数每 250 毫秒调用一次。该函数应该只包含实际的检查代码而不是加载代码。只需将 includeJs()
调用移出 testFx
函数即可。另请注意,testFx
函数必须 return 某些在您的示例中没有的东西,因此它始终未定义并在一段时间后超时。
page.open("http://example.com", function (status) {
if (status !== "success") {
console.log("Unable to access network");
} else {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
waitFor(function() {
return page.evaluate(function() {
return $("#some-element").is(":visible");
});
}, function() {
console.log("some-element should be visible now.");
phantom.exit();
});
});
}
});
我能够使用示例中提供的 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();
});
}
});
如您在 waitFor()
代码中所见,testFx
函数每 250 毫秒调用一次。该函数应该只包含实际的检查代码而不是加载代码。只需将 includeJs()
调用移出 testFx
函数即可。另请注意,testFx
函数必须 return 某些在您的示例中没有的东西,因此它始终未定义并在一段时间后超时。
page.open("http://example.com", function (status) {
if (status !== "success") {
console.log("Unable to access network");
} else {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
waitFor(function() {
return page.evaluate(function() {
return $("#some-element").is(":visible");
});
}, function() {
console.log("some-element should be visible now.");
phantom.exit();
});
});
}
});