added Casper.wait() for pausing step execution for a given amount of time
Showing
2 changed files
with
47 additions
and
8 deletions
... | @@ -163,14 +163,14 @@ | ... | @@ -163,14 +163,14 @@ |
163 | self.log(stepInfo + self.page.evaluate(function() { | 163 | self.log(stepInfo + self.page.evaluate(function() { |
164 | return document.location.href; | 164 | return document.location.href; |
165 | }) + ' (HTTP ' + self.currentHTTPStatus + ')', "info"); | 165 | }) + ' (HTTP ' + self.currentHTTPStatus + ')', "info"); |
166 | if (self.options.faultTolerant) { | 166 | try { |
167 | try { | 167 | step(self); |
168 | step(self); | 168 | } catch (e) { |
169 | } catch (e) { | 169 | if (self.options.faultTolerant) { |
170 | self.log("Step error: " + e, "error"); | 170 | self.log("Step error: " + e, "error"); |
171 | } else { | ||
172 | throw e; | ||
171 | } | 173 | } |
172 | } else { | ||
173 | step(self); | ||
174 | } | 174 | } |
175 | var time = new Date().getTime() - self.startTime; | 175 | var time = new Date().getTime() - self.startTime; |
176 | self.log(stepInfo + "done in " + time + "ms.", "info"); | 176 | self.log(stepInfo + "done in " + time + "ms.", "info"); |
... | @@ -634,6 +634,36 @@ | ... | @@ -634,6 +634,36 @@ |
634 | }, | 634 | }, |
635 | 635 | ||
636 | /** | 636 | /** |
637 | * Waits a given amount of time (expressed in milliseconds) before | ||
638 | * processing next step, which can passed as an optional argument. | ||
639 | * | ||
640 | * @param Number timeout The max amount of time to wait, in milliseconds | ||
641 | * @param Function then Next step to process (optional) | ||
642 | * @return Casper | ||
643 | */ | ||
644 | wait: function(timeout, then) { | ||
645 | if (typeof(timeout) !== "number" || Number(timeout, 10) < 1) { | ||
646 | this.die("wait() only accepts a positive integer > 0 as a timeout value"); | ||
647 | } | ||
648 | if (then && typeof(then) !== "function") { | ||
649 | this.die("wait() a step definition must be a function"); | ||
650 | } | ||
651 | this.delayedExecution = true; | ||
652 | var start = new Date().getTime(); | ||
653 | var interval = setInterval(function(self, then) { | ||
654 | if (!new Date().getTime() - start < timeout) { | ||
655 | self.delayedExecution = false; | ||
656 | self.log("wait() finished wating for " + timeout + "ms.", "info"); | ||
657 | if (then) { | ||
658 | self.then(then); | ||
659 | } | ||
660 | clearInterval(interval); | ||
661 | } | ||
662 | }, 100, this, then); | ||
663 | return this; | ||
664 | }, | ||
665 | |||
666 | /** | ||
637 | * Waits until a function returns true to process a next step. | 667 | * Waits until a function returns true to process a next step. |
638 | * | 668 | * |
639 | * @param Function testFx A function to be evaluated for returning condition satisfecit | 669 | * @param Function testFx A function to be evaluated for returning condition satisfecit |
... | @@ -645,10 +675,10 @@ | ... | @@ -645,10 +675,10 @@ |
645 | waitFor: function(testFx, then, onTimeout, timeout) { | 675 | waitFor: function(testFx, then, onTimeout, timeout) { |
646 | timeout = timeout ? timeout : this.defaultWaitTimeout; | 676 | timeout = timeout ? timeout : this.defaultWaitTimeout; |
647 | if (typeof testFx !== "function") { | 677 | if (typeof testFx !== "function") { |
648 | this.die("waitUntil() needs a test function"); | 678 | this.die("waitFor() needs a test function"); |
649 | } | 679 | } |
650 | if (then && typeof then !== "function") { | 680 | if (then && typeof then !== "function") { |
651 | this.die("waitUntil() next step definition must be a function"); | 681 | this.die("waitFor() next step definition must be a function"); |
652 | } | 682 | } |
653 | this.delayedExecution = true; | 683 | this.delayedExecution = true; |
654 | var start = new Date().getTime(); | 684 | var start = new Date().getTime(); | ... | ... |
... | @@ -136,8 +136,17 @@ casper.then(function(self) { | ... | @@ -136,8 +136,17 @@ casper.then(function(self) { |
136 | casper.options.verbose = true; | 136 | casper.options.verbose = true; |
137 | }); | 137 | }); |
138 | 138 | ||
139 | // Casper.wait | ||
140 | var start = new Date().getTime(); | ||
141 | casper.wait(1000); | ||
142 | casper.then(function(self) { | ||
143 | casper.test.comment('wait'); | ||
144 | self.test.assert(new Date().getTime() - start > 1000, 'Casper.wait() can wait for a given amount of time'); | ||
145 | }); | ||
146 | |||
139 | // Casper.waitFor | 147 | // Casper.waitFor |
140 | casper.thenOpen('tests/site/waitFor.html', function(self) { | 148 | casper.thenOpen('tests/site/waitFor.html', function(self) { |
149 | casper.test.comment('waitFor'); | ||
141 | self.waitFor(function(self) { | 150 | self.waitFor(function(self) { |
142 | return self.evaluate(function() { | 151 | return self.evaluate(function() { |
143 | return document.querySelectorAll('li').length === 4; | 152 | return document.querySelectorAll('li').length === 4; | ... | ... |
-
Please register or sign in to post a comment