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 ...@@ -110,6 +110,7 @@ Last, all the casper test suites have been upgraded to use the new testing featu
110 - 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 110 - 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
111 - fixes [#410](https://github.com/n1k0/casperjs/issues/410) - trigger `mousedown` and `mousedown` events on click 111 - fixes [#410](https://github.com/n1k0/casperjs/issues/410) - trigger `mousedown` and `mousedown` events on click
112 - Added [`Tester#skip`](http://docs.casperjs.org/en/latest/modules/tester.html#skip) method 112 - Added [`Tester#skip`](http://docs.casperjs.org/en/latest/modules/tester.html#skip) method
113 - Added [`Casper#eachThen()`](http://docs.casperjs.org/en/latest/modules/casper.html#eachThen)
113 114
114 2013-02-08, v1.0.2 115 2013-02-08, v1.0.2
115 ------------------ 116 ------------------
......
...@@ -659,6 +659,21 @@ Iterates over provided array items and execute a callback:: ...@@ -659,6 +659,21 @@ Iterates over provided array items and execute a callback::
659 659
660 Have a look at the `googlematch.js <https://github.com/n1k0/casperjs/blob/master/samples/googlematch.js>`_ sample script for a concrete use case. 660 Have a look at the `googlematch.js <https://github.com/n1k0/casperjs/blob/master/samples/googlematch.js>`_ sample script for a concrete use case.
661 661
662 ``eachThen()``
663 -------------------------------------------------------------------------------
664
665 **Signature:** ``eachThen(Array array, Function then)``
666
667 Iterates over provided array items and adds a step to the stack with current data attached to it::
668
669 casper.start().eachThen([1, 2, 3], function(response) {
670 this.echo(response.data);
671 }).run();
672
673 .. note::
674
675 Current item will be stored in the ``response.data`` property.
676
662 .. _casper_echo: 677 .. _casper_echo:
663 678
664 .. index:: echo, Printing 679 .. index:: echo, Printing
......
...@@ -567,7 +567,7 @@ Casper.prototype.download = function download(url, targetPath, method, data) { ...@@ -567,7 +567,7 @@ Casper.prototype.download = function download(url, targetPath, method, data) {
567 567
568 /** 568 /**
569 * Iterates over the values of a provided array and execute a callback 569 * Iterates over the values of a provided array and execute a callback
570 * for @ item. 570 * for each item.
571 * 571 *
572 * @param Array array 572 * @param Array array
573 * @param Function fn Callback: function(self, item, index) 573 * @param Function fn Callback: function(self, item, index)
...@@ -588,6 +588,32 @@ Casper.prototype.each = function each(array, fn) { ...@@ -588,6 +588,32 @@ Casper.prototype.each = function each(array, fn) {
588 }; 588 };
589 589
590 /** 590 /**
591 * Iterates over the values of a provided array and adds a step
592 * for each item.
593 *
594 * @param Array array
595 * @param Function then Step: function(response); item will be attached to response.data
596 * @return Casper
597 */
598 Casper.prototype.eachThen = function each(array, then) {
599 "use strict";
600 if (!utils.isArray(array)) {
601 this.log("each() only works with arrays", "error");
602 return this;
603 }
604 (function _each(self) {
605 array.forEach(function _forEach(item, i) {
606 self.then(function() {
607 this.then(this.createStep(then, {
608 data: item
609 }));
610 });
611 });
612 })(this);
613 return this;
614 };
615
616 /**
591 * Prints something to stdout. 617 * Prints something to stdout.
592 * 618 *
593 * @param String text A string to echo to stdout 619 * @param String text A string to echo to stdout
...@@ -1353,6 +1379,7 @@ Casper.prototype.run = function run(onComplete, time) { ...@@ -1353,6 +1379,7 @@ Casper.prototype.run = function run(onComplete, time) {
1353 */ 1379 */
1354 Casper.prototype.runStep = function runStep(step) { 1380 Casper.prototype.runStep = function runStep(step) {
1355 "use strict"; 1381 "use strict";
1382 /*jshint maxstatements:20*/
1356 this.checkStarted(); 1383 this.checkStarted();
1357 var skipLog = utils.isObject(step.options) && step.options.skipLog === true, 1384 var skipLog = utils.isObject(step.options) && step.options.skipLog === true,
1358 stepInfo = f("Step %d/%d", this.step, this.steps.length), 1385 stepInfo = f("Step %d/%d", this.step, this.steps.length),
...@@ -1381,6 +1408,9 @@ Casper.prototype.runStep = function runStep(step) { ...@@ -1381,6 +1408,9 @@ Casper.prototype.runStep = function runStep(step) {
1381 }, this.options.stepTimeout, this, new Date().getTime(), getCurrentSuiteId(this)); 1408 }, this.options.stepTimeout, this, new Date().getTime(), getCurrentSuiteId(this));
1382 } 1409 }
1383 this.emit('step.start', step); 1410 this.emit('step.start', step);
1411 if (this.currentResponse) {
1412 this.currentResponse.data = step.options && step.options.data || null;
1413 }
1384 try { 1414 try {
1385 stepResult = step.call(this, this.currentResponse); 1415 stepResult = step.call(this, this.currentResponse);
1386 if (utils.isFunction(this.options.onStepComplete)) { 1416 if (utils.isFunction(this.options.onStepComplete)) {
......
...@@ -35,3 +35,17 @@ casper.test.begin('steps tests', 8, function(test) { ...@@ -35,3 +35,17 @@ casper.test.begin('steps tests', 8, function(test) {
35 test.done(); 35 test.done();
36 }); 36 });
37 }); 37 });
38
39 casper.test.begin('eachThen() tests', 1, function(test) {
40 var received = [];
41
42 casper.start().eachThen([1, 2, 3], function(response) {
43 received.push(response.data);
44 });
45
46 casper.run(function() {
47 test.assertEquals(received, [1, 2, 3],
48 'Casper.eachThen() passes item to step data');
49 test.done();
50 });
51 });
......