Commit 9d4432d9 9d4432d9644d0d948260eb286825560ec58d6e1d by Nicolas Perriault

added Casper#waitFor() and Casper#waitForSelector()

1 parent 6cbb22c8
.DS_Store
*.xml
*.png
......
......@@ -58,6 +58,8 @@
this.colorizer = new phantom.Casper.Colorizer();
this.currentUrl = 'about:blank';
this.currentHTTPStatus = 200;
this.defaultWaitTimeout = 5000;
this.delayedExecution = false;
this.loadInProgress = false;
this.logLevels = ["debug", "info", "warning", "error"];
this.logStyles = {
......@@ -174,7 +176,7 @@
self.log(stepInfo + "done in " + time + "ms.", "info");
self.step++;
}
if (typeof step !== "function") {
if (typeof step !== "function" && !self.delayedExecution) {
self.result.time = new Date().getTime() - self.startTime;
self.log("Done " + self.steps.length + " steps in " + self.result.time + 'ms.', "info");
clearInterval(self.checker);
......@@ -629,6 +631,64 @@
*/
thenOpenAndEvaluate: function(location, fn, replacements) {
return this.thenOpen(location).thenEvaluate(fn, replacements);
},
/**
* Waits until a function returns true to process a next step.
*
* @param Function testFx A function to be evaluated for returning condition satisfecit
* @param Function then The next step to perform
* @param Number timeout The max amount of time to wait, in milliseconds
* @return Casper
*/
waitFor: function(testFx, then, onTimeout, timeout) {
timeout = timeout ? timeout : this.defaultWaitTimeout;
if (typeof testFx !== "function") {
this.die("waitUntil() needs a test function");
}
if (typeof then !== "function") {
this.die("waitUntil() needs a next step definition");
}
this.delayedExecution = true;
var start = new Date().getTime();
var condition = false;
var interval = setInterval(function(self, testFx, onTimeout) {
if ((new Date().getTime() - start < timeout) && !condition) {
condition = testFx(self);
} else {
self.delayedExecution = false;
if (!condition) {
self.log("Casper.waitFor() timeout", "warning");
if (typeof onTimeout === "function") {
onTimeout(self);
} else {
self.die("Expired timeout, exiting.", "error");
}
clearInterval(interval);
} else {
self.log("waitFor() finished in " + (new Date().getTime() - start) + "ms.", "info");
self.then(then);
clearInterval(interval);
}
}
}, 100, this, testFx, onTimeout);
return this;
},
/**
* Waits until an element matching the provided CSS3 selector exists in
* remote DOM to process a next step.
*
* @param String selector A CSS3 selector
* @param Function then The next step to perform
* @param Number timeout The max amount of time to wait, in milliseconds
* @return Casper
*/
waitForSelector: function(selector, then, onTimeout, timeout) {
timeout = timeout ? timeout : this.defaultWaitTimeout;
return this.waitFor(function(self) {
return self.exists(selector);
}, then, onTimeout, timeout);
}
};
......@@ -1169,6 +1229,15 @@
};
/**
* Adds a failed test entry to the stack.
*
* @param String message
*/
this.fail = function(message) {
this.assert(false, message);
};
/**
* Formats a message to highlight some parts of it.
*
* @param String message
......@@ -1192,6 +1261,15 @@
};
/**
* Adds a successful test entry to the stack.
*
* @param String message
*/
this.pass = function(message) {
this.assert(true, message);
};
/**
* Render tests results, an optionnaly exit phantomjs.
*
* @param Boolean exit
......
phantom.injectJs('casper.js');
var casper = new phantom.Casper({
logLevel: "debug",
verbose: true
});
casper.start('https://twitter.com/#!/twilio', function(self) {
self.waitForSelector('.tweet-row', function(self) {
self.captureSelector('twitter.png', 'html');
}, null, 12000);
});
casper.run(function(self) {
self.exit();
});
\ No newline at end of file
......@@ -136,6 +136,19 @@ casper.then(function(self) {
casper.options.verbose = true;
});
// Casper.waitFor
casper.thenOpen('tests/site/waitFor.html', function(self) {
self.waitFor(function(self) {
return self.evaluate(function() {
return document.querySelectorAll('li').length === 4;
});
}, function(self) {
self.test.pass('Casper.waitFor() can wait for something to happen');
}, function(self) {
self.test.fail('Casper.waitFor() can wait for something to happen');
});
});
// run suite
casper.run(function(self) {
self.test.assertEquals(self.result.log.length, 3, 'log() logged messages');
......
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>waitFor test</title>
</head>
<body>
<img src="images/phantom.png"/>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<script>
setTimeout(function() {
var li = document.createElement('li')
li.appendChild(document.createTextNode('four'));
document.querySelector('ul').appendChild(li);
}, 2000);
</script>
</body>
</html>
\ No newline at end of file