Commit 842e9089 842e9089be47aec266d736e71eacc4e765e7178c by Nicolas Perriault

added Casper#eachThen()

1 parent be5c0460
......@@ -110,6 +110,7 @@ Last, all the casper test suites have been upgraded to use the new testing featu
- Added [`Casper#bypass`](http://docs.casperjs.org/en/latest/modules/casper.html#bypass), [`Casper#thenBypass`](http://docs.casperjs.org/en/latest/modules/casper.html#thenbypass), [`Casper#thenBypassIf`](http://docs.casperjs.org/en/latest/modules/casper.html#thenbypassif), [`Casper#thenBypassUnless`](http://docs.casperjs.org/en/latest/modules/casper.html#thenbypassunless) methods
- fixes [#410](https://github.com/n1k0/casperjs/issues/410) - trigger `mousedown` and `mousedown` events on click
- Added [`Tester#skip`](http://docs.casperjs.org/en/latest/modules/tester.html#skip) method
- Added [`Casper#eachThen()`](http://docs.casperjs.org/en/latest/modules/casper.html#eachThen)
2013-02-08, v1.0.2
------------------
......
......@@ -659,6 +659,21 @@ Iterates over provided array items and execute a callback::
Have a look at the `googlematch.js <https://github.com/n1k0/casperjs/blob/master/samples/googlematch.js>`_ sample script for a concrete use case.
``eachThen()``
-------------------------------------------------------------------------------
**Signature:** ``eachThen(Array array, Function then)``
Iterates over provided array items and adds a step to the stack with current data attached to it::
casper.start().eachThen([1, 2, 3], function(response) {
this.echo(response.data);
}).run();
.. note::
Current item will be stored in the ``response.data`` property.
.. _casper_echo:
.. index:: echo, Printing
......
......@@ -567,7 +567,7 @@ Casper.prototype.download = function download(url, targetPath, method, data) {
/**
* Iterates over the values of a provided array and execute a callback
* for @ item.
* for each item.
*
* @param Array array
* @param Function fn Callback: function(self, item, index)
......@@ -588,6 +588,32 @@ Casper.prototype.each = function each(array, fn) {
};
/**
* Iterates over the values of a provided array and adds a step
* for each item.
*
* @param Array array
* @param Function then Step: function(response); item will be attached to response.data
* @return Casper
*/
Casper.prototype.eachThen = function each(array, then) {
"use strict";
if (!utils.isArray(array)) {
this.log("each() only works with arrays", "error");
return this;
}
(function _each(self) {
array.forEach(function _forEach(item, i) {
self.then(function() {
this.then(this.createStep(then, {
data: item
}));
});
});
})(this);
return this;
};
/**
* Prints something to stdout.
*
* @param String text A string to echo to stdout
......@@ -1353,6 +1379,7 @@ Casper.prototype.run = function run(onComplete, time) {
*/
Casper.prototype.runStep = function runStep(step) {
"use strict";
/*jshint maxstatements:20*/
this.checkStarted();
var skipLog = utils.isObject(step.options) && step.options.skipLog === true,
stepInfo = f("Step %d/%d", this.step, this.steps.length),
......@@ -1381,6 +1408,9 @@ Casper.prototype.runStep = function runStep(step) {
}, this.options.stepTimeout, this, new Date().getTime(), getCurrentSuiteId(this));
}
this.emit('step.start', step);
if (this.currentResponse) {
this.currentResponse.data = step.options && step.options.data || null;
}
try {
stepResult = step.call(this, this.currentResponse);
if (utils.isFunction(this.options.onStepComplete)) {
......
......@@ -35,3 +35,17 @@ casper.test.begin('steps tests', 8, function(test) {
test.done();
});
});
casper.test.begin('eachThen() tests', 1, function(test) {
var received = [];
casper.start().eachThen([1, 2, 3], function(response) {
received.push(response.data);
});
casper.run(function() {
test.assertEquals(received, [1, 2, 3],
'Casper.eachThen() passes item to step data');
test.done();
});
});
......