Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/webpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,41 @@
setTimeout(testForSelector, timeoutInterval);
},

//waits for selector to appear, then executes callbackFn
waitForNthSelector:function (selector, count, callbackFn, timeout) {
var self = this,
startTime = Date.now(),
timeoutInterval = 150,
testRunning = false,
//if evaluate succeeds, invokes callback w/ true, if timeout,
// invokes w/ false, otherwise just exits
testForSelector = function () {

var elapsedTime = Date.now() - startTime;

if (elapsedTime > timeout) {
self.options.debug && console.log('warning: timeout occurred while waiting for selector:"%s"'.yellow, selector);
callbackFn(false);
return;
}

self.evaluate(function (selectorToEvaluate) {
return document.querySelectorAll(selectorToEvaluate).length;
}, function (result) {
testRunning = false;
if (result >= count) {//selector found
callbackFn(true);
}
else {
setTimeout(testForSelector, timeoutInterval);
}
}, selector);
};

timeout = timeout || 10000; //default timeout is 2 sec;
setTimeout(testForSelector, timeoutInterval);
},

injectJs:function (filename, callbackFn) {

executeMethod.call(this, '/page/functions/injectJs', callbackFn, filename);
Expand Down