Update tester to output tailored messages for waitFor.timeouts
Showing
3 changed files
with
167 additions
and
13 deletions
... | @@ -164,15 +164,6 @@ var Tester = function Tester(casper, options) { | ... | @@ -164,15 +164,6 @@ var Tester = function Tester(casper, options) { |
164 | } | 164 | } |
165 | }); | 165 | }); |
166 | 166 | ||
167 | // casper events | ||
168 | this.casper.on('error', function onCasperError(msg, backtrace) { | ||
169 | self.processPhantomError(msg, backtrace); | ||
170 | }); | ||
171 | |||
172 | this.casper.on('waitFor.timeout', function onWaitForTimeout(timeout) { | ||
173 | this.warn(f('wait timeout of %dms reached', timeout)); | ||
174 | }); | ||
175 | |||
176 | function errorHandler(error, backtrace) { | 167 | function errorHandler(error, backtrace) { |
177 | self.casper.unwait(); | 168 | self.casper.unwait(); |
178 | if (error instanceof Error) { | 169 | if (error instanceof Error) { |
... | @@ -190,12 +181,46 @@ var Tester = function Tester(casper, options) { | ... | @@ -190,12 +181,46 @@ var Tester = function Tester(casper, options) { |
190 | } catch (e) {} | 181 | } catch (e) {} |
191 | self.uncaughtError(error, self.currentTestFile, line, backtrace); | 182 | self.uncaughtError(error, self.currentTestFile, line, backtrace); |
192 | } | 183 | } |
193 | 184 | ||
194 | function errorHandlerAndDone(error, backtrace) { | 185 | function errorHandlerAndDone(error, backtrace) { |
195 | errorHandler(error, backtrace); | 186 | errorHandler(error, backtrace); |
196 | self.done(); | 187 | self.done(); |
197 | } | 188 | } |
198 | 189 | ||
190 | // casper events | ||
191 | this.casper.on('error', function onCasperError(msg, backtrace) { | ||
192 | self.processPhantomError(msg, backtrace); | ||
193 | }); | ||
194 | |||
195 | this.casper.on('waitFor.timeout', function onWaitForTimeout(timeout, details) { | ||
196 | var message = f("Wait timeout occured (%dms)", timeout); | ||
197 | details = details || {}; | ||
198 | |||
199 | if (details.selector) { | ||
200 | message = f(details.waitWhile ? '"%s" never went away in %dms' : '"%s" still did not exist in %dms', details.selector, timeout); | ||
201 | } | ||
202 | else if (details.visible) { | ||
203 | message = f(details.waitWhile ? '"%s" never disappeared in %dms' : '"%s" never appeared in %dms', details.visible, timeout); | ||
204 | } | ||
205 | else if (details.url || details.resource) { | ||
206 | message = f('%s did not load in %dms', details.url || details.resource, timeout); | ||
207 | } | ||
208 | else if (details.popup) { | ||
209 | message = f('%s did not pop up in %dms', details.popup, timeout); | ||
210 | } | ||
211 | else if (details.text) { | ||
212 | message = f('"%s" did not appear in the page in %dms', details.text, timeout); | ||
213 | } | ||
214 | else if (details.selectorTextChange) { | ||
215 | message = f('"%s" did not have a text change in %dms', details.selectorTextChange, timeout); | ||
216 | } | ||
217 | else if (utils.isFunction(details.testFx)) { | ||
218 | message = f('"%s" did not evaluate to something truthy in %dms', details.testFx.toString(), timeout); | ||
219 | } | ||
220 | |||
221 | errorHandlerAndDone(new TimedOutError(message)); | ||
222 | }); | ||
223 | |||
199 | [ | 224 | [ |
200 | 'wait.error', | 225 | 'wait.error', |
201 | 'waitFor.timeout.error', | 226 | 'waitFor.timeout.error', |
... | @@ -227,9 +252,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -227,9 +252,7 @@ var Tester = function Tester(casper, options) { |
227 | throw new TimedOutError(f("Timeout occured (%dms)", timeout)); | 252 | throw new TimedOutError(f("Timeout occured (%dms)", timeout)); |
228 | }; | 253 | }; |
229 | 254 | ||
230 | this.casper.options.onWaitTimeout = function test_onWaitTimeout(timeout) { | 255 | this.casper.options.onWaitTimeout = function () {}; |
231 | throw new TimedOutError(f("Wait timeout occured (%dms)", timeout)); | ||
232 | }; | ||
233 | }; | 256 | }; |
234 | 257 | ||
235 | // Tester class is an EventEmitter | 258 | // Tester class is an EventEmitter | ... | ... |
... | @@ -287,6 +287,21 @@ class TestCommandOutputTest(CasperExecTestBase): | ... | @@ -287,6 +287,21 @@ class TestCommandOutputTest(CasperExecTestBase): |
287 | ], failing=True) | 287 | ], failing=True) |
288 | 288 | ||
289 | @timeout(20) | 289 | @timeout(20) |
290 | def test_waitFor_timeout(self): | ||
291 | # using begin() | ||
292 | script_path = os.path.join(TEST_ROOT, 'tester', 'waitFor_timeout.js') | ||
293 | self.assertCommandOutputContains('test ' + script_path, [ | ||
294 | '"p.nonexistent" still did not exist in', | ||
295 | '"#encoded" did not have a text change in', | ||
296 | '"p[style]" never appeared in', | ||
297 | '/github\.com/ did not load in', | ||
298 | '/foobar/ did not pop up in', | ||
299 | '"Lorem ipsum" did not appear in the page in', | ||
300 | 'return false', | ||
301 | 'did not evaluate to something truthy in' | ||
302 | ], failing=True) | ||
303 | |||
304 | @timeout(20) | ||
290 | def test_dubious_test(self): | 305 | def test_dubious_test(self): |
291 | script_path = os.path.join(TEST_ROOT, 'tester', 'dubious.js') | 306 | script_path = os.path.join(TEST_ROOT, 'tester', 'dubious.js') |
292 | self.assertCommandOutputContains('test ' + script_path, [ | 307 | self.assertCommandOutputContains('test ' + script_path, [ | ... | ... |
tests/clitests/tester/waitFor_timeout.js
0 → 100644
1 | /*global casper*/ | ||
2 | /*jshint strict:false*/ | ||
3 | |||
4 | casper.options.waitTimeout = 500; | ||
5 | |||
6 | casper.test.begin('waitForSelector fails with expected message', 1, function(test) { | ||
7 | casper.start('../site/waitFor.html'); | ||
8 | |||
9 | casper.waitForSelector('p.nonexistent', function() { | ||
10 | throw new Error('waitForSelector found something it should not have'); | ||
11 | }); | ||
12 | |||
13 | casper.run(function() { | ||
14 | test.done(); | ||
15 | }); | ||
16 | }); | ||
17 | |||
18 | casper.test.begin('waitWhileSelector fails with expected message', 1, function(test) { | ||
19 | casper.start('../site/waitFor.html'); | ||
20 | |||
21 | casper.waitForSelector('#encoded'); | ||
22 | |||
23 | casper.waitWhileSelector('#encoded', function() { | ||
24 | throw new Error('waitWhileSelector thought something got removed when it did not'); | ||
25 | }); | ||
26 | |||
27 | casper.run(function() { | ||
28 | test.done(); | ||
29 | }); | ||
30 | }); | ||
31 | |||
32 | casper.test.begin('waitForSelectorTextChange fails with expected message', 1, function(test) { | ||
33 | casper.start('../site/waitFor.html'); | ||
34 | |||
35 | casper.waitForSelectorTextChange('#encoded', function() { | ||
36 | throw new Error('waitForSelectorTextChange thought text changed when it did not'); | ||
37 | }); | ||
38 | |||
39 | casper.run(function() { | ||
40 | test.done(); | ||
41 | }); | ||
42 | }); | ||
43 | |||
44 | casper.test.begin('waitUntilVisible fails with expected message', 1, function(test) { | ||
45 | casper.start('../site/waitFor.html'); | ||
46 | |||
47 | casper.waitUntilVisible('p[style]', function() { | ||
48 | throw new Error('waitUntilVisible falsely identified a hidden paragraph'); | ||
49 | }); | ||
50 | |||
51 | casper.run(function() { | ||
52 | test.done(); | ||
53 | }); | ||
54 | }); | ||
55 | |||
56 | casper.test.begin('waitWhileVisible fails with expected message', 1, function(test) { | ||
57 | casper.start('../site/waitFor.html'); | ||
58 | |||
59 | casper.waitWhileVisible('img', function() { | ||
60 | throw new Error('waitWhileVisible thought something disappeared when it did not'); | ||
61 | }); | ||
62 | |||
63 | casper.run(function() { | ||
64 | test.done(); | ||
65 | }); | ||
66 | }); | ||
67 | |||
68 | casper.test.begin('waitForUrl fails with expected message', 1, function(test) { | ||
69 | casper.start('../site/waitFor.html'); | ||
70 | |||
71 | casper.waitForUrl(/github\.com/, function() { | ||
72 | throw new Error('waitForUrl thought we actually navigated to GitHub'); | ||
73 | }); | ||
74 | |||
75 | casper.run(function() { | ||
76 | test.done(); | ||
77 | }); | ||
78 | }); | ||
79 | |||
80 | casper.test.begin('waitForPopup fails with expected message', 1, function(test) { | ||
81 | casper.start('../site/waitFor.html'); | ||
82 | |||
83 | casper.waitForPopup(/foobar/, function() { | ||
84 | throw new Error('waitForPopup found something it should not have'); | ||
85 | }); | ||
86 | |||
87 | casper.run(function() { | ||
88 | test.done(); | ||
89 | }); | ||
90 | }); | ||
91 | |||
92 | casper.test.begin('waitForText fails with expected message', 1, function(test) { | ||
93 | casper.start('../site/waitFor.html'); | ||
94 | |||
95 | casper.waitForText("Lorem ipsum", function() { | ||
96 | throw new Error('waitForText found something it should not have'); | ||
97 | }); | ||
98 | |||
99 | casper.run(function() { | ||
100 | test.done(); | ||
101 | }); | ||
102 | }); | ||
103 | |||
104 | casper.test.begin('waitFor fails with expected message', 1, function(test) { | ||
105 | casper.start('../site/waitFor.html'); | ||
106 | |||
107 | casper.waitFor(function() { | ||
108 | return false | ||
109 | }, function() { | ||
110 | throw new Error('waitFor fasely succeeded'); | ||
111 | }); | ||
112 | |||
113 | casper.run(function() { | ||
114 | test.done(); | ||
115 | }); | ||
116 | }); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or sign in to post a comment