added Casper.repeat() method for easy definition of repeated steps
Showing
1 changed file
with
20 additions
and
4 deletions
... | @@ -104,14 +104,15 @@ | ... | @@ -104,14 +104,15 @@ |
104 | * @param function onComplete An options callback to apply on completion | 104 | * @param function onComplete An options callback to apply on completion |
105 | */ | 105 | */ |
106 | checkStep: function(self, onComplete) { | 106 | checkStep: function(self, onComplete) { |
107 | if (!self.loadInProgress && typeof(self.steps[self.step]) === "function") { | 107 | var step = self.steps[self.step]; |
108 | if (!self.loadInProgress && typeof(step) === "function") { | ||
108 | var curStepNum = self.step + 1 | 109 | var curStepNum = self.step + 1 |
109 | , stepInfo = "Step " + curStepNum + "/" + self.steps.length + ": "; | 110 | , stepInfo = "Step " + curStepNum + "/" + self.steps.length + ": "; |
110 | self.log(stepInfo + self.page.evaluate(function() { | 111 | self.log(stepInfo + self.page.evaluate(function() { |
111 | return document.location.href; | 112 | return document.location.href; |
112 | }) + ' (HTTP ' + self.currentHTTPStatus + ')', "info"); | 113 | }) + ' (HTTP ' + self.currentHTTPStatus + ')', "info"); |
113 | try { | 114 | try { |
114 | self.steps[self.step](self); | 115 | step(self); |
115 | } catch (e) { | 116 | } catch (e) { |
116 | self.log("Fatal: " + e, "error"); | 117 | self.log("Fatal: " + e, "error"); |
117 | } | 118 | } |
... | @@ -119,7 +120,7 @@ | ... | @@ -119,7 +120,7 @@ |
119 | self.log(stepInfo + "done in " + time + "ms.", "info"); | 120 | self.log(stepInfo + "done in " + time + "ms.", "info"); |
120 | self.step++; | 121 | self.step++; |
121 | } | 122 | } |
122 | if (typeof(self.steps[self.step]) !== "function") { | 123 | if (typeof(step) !== "function") { |
123 | self.result.time = new Date().getTime() - self.startTime; | 124 | self.result.time = new Date().getTime() - self.startTime; |
124 | self.log("Done " + self.steps.length + " steps in " + self.result.time + 'ms.', "info"); | 125 | self.log("Done " + self.steps.length + " steps in " + self.result.time + 'ms.', "info"); |
125 | clearInterval(self.checker); | 126 | clearInterval(self.checker); |
... | @@ -189,7 +190,7 @@ | ... | @@ -189,7 +190,7 @@ |
189 | * WebPage#evaluate does, but can also replace values by their | 190 | * WebPage#evaluate does, but can also replace values by their |
190 | * placeholer names: | 191 | * placeholer names: |
191 | * | 192 | * |
192 | * navigator.evaluate(function() { | 193 | * casper.evaluate(function() { |
193 | * document.querySelector('#username').setAttribute('value', '%username%'); | 194 | * document.querySelector('#username').setAttribute('value', '%username%'); |
194 | * document.querySelector('#password').setAttribute('value', '%password%'); | 195 | * document.querySelector('#password').setAttribute('value', '%password%'); |
195 | * document.querySelector('#submit').click(); | 196 | * document.querySelector('#submit').click(); |
... | @@ -287,6 +288,21 @@ | ... | @@ -287,6 +288,21 @@ |
287 | }, | 288 | }, |
288 | 289 | ||
289 | /** | 290 | /** |
291 | * Repeats a step a given number of times. | ||
292 | * | ||
293 | * @param Number times Number of times to repeat step | ||
294 | * @aram function step The step closure | ||
295 | * @return Casper | ||
296 | * @see Casper#then | ||
297 | */ | ||
298 | repeat: function(times, step) { | ||
299 | for (var i = 0; i < times; i++) { | ||
300 | this.then(step); | ||
301 | } | ||
302 | return this; | ||
303 | }, | ||
304 | |||
305 | /** | ||
290 | * Runs the whole suite of steps. | 306 | * Runs the whole suite of steps. |
291 | * | 307 | * |
292 | * @param function onComplete an optional callback | 308 | * @param function onComplete an optional callback | ... | ... |
-
Please register or sign in to post a comment