added Casper#waitForAlert()
Showing
3 changed files
with
57 additions
and
0 deletions
... | @@ -2025,6 +2025,19 @@ Example using the ``onTimeout`` callback:: | ... | @@ -2025,6 +2025,19 @@ Example using the ``onTimeout`` callback:: |
2025 | ``details`` is a property bag of various information that will be passed to the ``waitFor.timeout`` event, if it is emitted. | 2025 | ``details`` is a property bag of various information that will be passed to the ``waitFor.timeout`` event, if it is emitted. |
2026 | This can be used for better error messages or to conditionally ignore some timeout events. | 2026 | This can be used for better error messages or to conditionally ignore some timeout events. |
2027 | 2027 | ||
2028 | .. index:: alert | ||
2029 | |||
2030 | ``waitForAlert()`` | ||
2031 | ------------------------------------------------------------------------------- | ||
2032 | |||
2033 | **Signature:** ``waitForAlert(Function then[, Function onTimeout, Number timeout])`` | ||
2034 | |||
2035 | Waits until a `JavaScript alert <https://developer.mozilla.org/en-US/docs/Web/API/Window.alert>`_ is triggered. The step function will be passed the alert message in the ``response.data`` property:: | ||
2036 | |||
2037 | casper.waitForAlert(function(response) { | ||
2038 | this.echo("Alert received: " + response.data); | ||
2039 | }); | ||
2040 | |||
2028 | .. _casper_waitforpopup: | 2041 | .. _casper_waitforpopup: |
2029 | 2042 | ||
2030 | .. index:: Popups, New window, window.open, Tabs | 2043 | .. index:: Popups, New window, window.open, Tabs | ... | ... |
... | @@ -2118,6 +2118,31 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout, de | ... | @@ -2118,6 +2118,31 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout, de |
2118 | }; | 2118 | }; |
2119 | 2119 | ||
2120 | /** | 2120 | /** |
2121 | * Waits until any alert is triggered. | ||
2122 | * | ||
2123 | * @param Function then The next step to perform (required) | ||
2124 | * @param Function onTimeout A callback function to call on timeout (optional) | ||
2125 | * @param Number timeout The max amount of time to wait, in milliseconds (optional) | ||
2126 | * @return Casper | ||
2127 | */ | ||
2128 | Casper.prototype.waitForAlert = function(then, onTimeout, timeout) { | ||
2129 | "use strict"; | ||
2130 | if (!utils.isFunction(then)) { | ||
2131 | throw new CasperError("waitForAlert() needs a step function"); | ||
2132 | } | ||
2133 | var message; | ||
2134 | function alertCallback(msg) { | ||
2135 | message = msg; | ||
2136 | } | ||
2137 | this.once("remote.alert", alertCallback); | ||
2138 | return this.waitFor(function isAlertReceived() { | ||
2139 | return message !== undefined; | ||
2140 | }, function onAlertReceived() { | ||
2141 | this.then(this.createStep(then, {data: message})); | ||
2142 | }, onTimeout, timeout); | ||
2143 | }; | ||
2144 | |||
2145 | /** | ||
2121 | * Waits for a popup page having its url matching the provided pattern to be opened | 2146 | * Waits for a popup page having its url matching the provided pattern to be opened |
2122 | * and loaded. | 2147 | * and loaded. |
2123 | * | 2148 | * | ... | ... |
... | @@ -23,3 +23,22 @@ casper.test.begin('alert events', 1, { | ... | @@ -23,3 +23,22 @@ casper.test.begin('alert events', 1, { |
23 | }); | 23 | }); |
24 | } | 24 | } |
25 | }); | 25 | }); |
26 | |||
27 | casper.test.begin("Casper.waitForAlert() waits for an alert", 1, function(test) { | ||
28 | casper.start().then(function() { | ||
29 | this.evaluate(function() { | ||
30 | setTimeout(function() { | ||
31 | alert("plop"); | ||
32 | }, 500); | ||
33 | }); | ||
34 | }); | ||
35 | |||
36 | casper.waitForAlert(function(response) { | ||
37 | test.assertEquals(response.data, "plop", | ||
38 | "Casper.waitForAlert() can wait for an alert to be triggered"); | ||
39 | }); | ||
40 | |||
41 | casper.run(function() { | ||
42 | test.done(); | ||
43 | }); | ||
44 | }); | ... | ... |
-
Please register or sign in to post a comment