diff options
author | Ian Coleman <coleman.ian@gmail.com> | 2016-08-23 17:45:23 +1000 |
---|---|---|
committer | Ian Coleman <coleman.ian@gmail.com> | 2016-08-23 17:47:48 +1000 |
commit | 3eef9d0d36adf3e1034fe7a11ccd4802505218ca (patch) | |
tree | 84b828785f5031fc9996c25002ab9466f02fed1a | |
parent | ec60b6624a5f84943ffb6e6be8b3af9c2274097b (diff) | |
download | BIP39-3eef9d0d36adf3e1034fe7a11ccd4802505218ca.tar.gz BIP39-3eef9d0d36adf3e1034fe7a11ccd4802505218ca.tar.zst BIP39-3eef9d0d36adf3e1034fe7a11ccd4802505218ca.zip |
Tests use waitForGenerate, not magical setTimeout
-rw-r--r-- | tests.js | 89 |
1 files changed, 59 insertions, 30 deletions
@@ -4,6 +4,7 @@ | |||
4 | 4 | ||
5 | var page = require('webpage').create(); | 5 | var page = require('webpage').create(); |
6 | var url = 'src/index.html'; | 6 | var url = 'src/index.html'; |
7 | var testMaxTime = 5000; | ||
7 | 8 | ||
8 | page.onResourceError = function(e) { | 9 | page.onResourceError = function(e) { |
9 | console.log("Error loading " + e.url); | 10 | console.log("Error loading " + e.url); |
@@ -15,6 +16,34 @@ function fail() { | |||
15 | phantom.exit(); | 16 | phantom.exit(); |
16 | } | 17 | } |
17 | 18 | ||
19 | function waitForGenerate(fn, maxTime) { | ||
20 | if (!maxTime) { | ||
21 | maxTime = testMaxTime; | ||
22 | } | ||
23 | var start = new Date().getTime(); | ||
24 | var prevAddressCount = -1; | ||
25 | var wait = function keepWaiting() { | ||
26 | var now = new Date().getTime(); | ||
27 | var hasTimedOut = now - start > maxTime; | ||
28 | var addressCount = page.evaluate(function() { | ||
29 | return $(".address").length; | ||
30 | }); | ||
31 | var hasFinished = addressCount > 0 && addressCount == prevAddressCount; | ||
32 | prevAddressCount = addressCount; | ||
33 | if (hasFinished) { | ||
34 | fn(); | ||
35 | } | ||
36 | else if (hasTimedOut) { | ||
37 | console.log("Test timed out"); | ||
38 | fn(); | ||
39 | } | ||
40 | else { | ||
41 | setTimeout(keepWaiting, 100); | ||
42 | } | ||
43 | } | ||
44 | wait(); | ||
45 | } | ||
46 | |||
18 | function next() { | 47 | function next() { |
19 | if (tests.length > 0) { | 48 | if (tests.length > 0) { |
20 | var testsStr = tests.length == 1 ? "test" : "tests"; | 49 | var testsStr = tests.length == 1 ? "test" : "tests"; |
@@ -63,7 +92,7 @@ page.open(url, function(status) { | |||
63 | $(".phrase").val("abandon abandon ability").trigger("input"); | 92 | $(".phrase").val("abandon abandon ability").trigger("input"); |
64 | }); | 93 | }); |
65 | // get the address | 94 | // get the address |
66 | setTimeout(function() { | 95 | waitForGenerate(function() { |
67 | var actual = page.evaluate(function() { | 96 | var actual = page.evaluate(function() { |
68 | return $(".address:first").text(); | 97 | return $(".address:first").text(); |
69 | }); | 98 | }); |
@@ -74,7 +103,7 @@ page.open(url, function(status) { | |||
74 | fail(); | 103 | fail(); |
75 | } | 104 | } |
76 | next(); | 105 | next(); |
77 | }, 1000); | 106 | }); |
78 | }); | 107 | }); |
79 | }, | 108 | }, |
80 | 109 | ||
@@ -94,7 +123,7 @@ page.open(url, function(status) { | |||
94 | $(".generate").click(); | 123 | $(".generate").click(); |
95 | }); | 124 | }); |
96 | // get the new phrase | 125 | // get the new phrase |
97 | setTimeout(function() { | 126 | waitForGenerate(function() { |
98 | var phrase = page.evaluate(function() { | 127 | var phrase = page.evaluate(function() { |
99 | return $(".phrase").val(); | 128 | return $(".phrase").val(); |
100 | }); | 129 | }); |
@@ -103,7 +132,7 @@ page.open(url, function(status) { | |||
103 | fail(); | 132 | fail(); |
104 | } | 133 | } |
105 | next(); | 134 | next(); |
106 | }, 1000); | 135 | }); |
107 | }); | 136 | }); |
108 | }, | 137 | }, |
109 | 138 | ||
@@ -121,7 +150,7 @@ page.open(url, function(status) { | |||
121 | $(".generate").click(); | 150 | $(".generate").click(); |
122 | }); | 151 | }); |
123 | // check the new phrase is six words long | 152 | // check the new phrase is six words long |
124 | setTimeout(function() { | 153 | waitForGenerate(function() { |
125 | var actualLength = page.evaluate(function() { | 154 | var actualLength = page.evaluate(function() { |
126 | var words = $(".phrase").val().split(" "); | 155 | var words = $(".phrase").val().split(" "); |
127 | return words.length; | 156 | return words.length; |
@@ -133,7 +162,7 @@ page.open(url, function(status) { | |||
133 | fail(); | 162 | fail(); |
134 | } | 163 | } |
135 | next(); | 164 | next(); |
136 | }, 1000); | 165 | }); |
137 | }); | 166 | }); |
138 | }, | 167 | }, |
139 | 168 | ||
@@ -147,7 +176,7 @@ page.open(url, function(status) { | |||
147 | $(".passphrase").val("secure_passphrase").trigger("input"); | 176 | $(".passphrase").val("secure_passphrase").trigger("input"); |
148 | }); | 177 | }); |
149 | // check the address is generated correctly | 178 | // check the address is generated correctly |
150 | setTimeout(function() { | 179 | waitForGenerate(function() { |
151 | var actual = page.evaluate(function() { | 180 | var actual = page.evaluate(function() { |
152 | return $(".address:first").text(); | 181 | return $(".address:first").text(); |
153 | }); | 182 | }); |
@@ -158,7 +187,7 @@ page.open(url, function(status) { | |||
158 | fail(); | 187 | fail(); |
159 | } | 188 | } |
160 | next(); | 189 | next(); |
161 | }, 1000); | 190 | }); |
162 | }); | 191 | }); |
163 | }, | 192 | }, |
164 | 193 | ||
@@ -175,7 +204,7 @@ page.open(url, function(status) { | |||
175 | $(".network").trigger("change"); | 204 | $(".network").trigger("change"); |
176 | }); | 205 | }); |
177 | // check the address is generated correctly | 206 | // check the address is generated correctly |
178 | setTimeout(function() { | 207 | waitForGenerate(function() { |
179 | var actual = page.evaluate(function() { | 208 | var actual = page.evaluate(function() { |
180 | return $(".address:first").text(); | 209 | return $(".address:first").text(); |
181 | }); | 210 | }); |
@@ -186,7 +215,7 @@ page.open(url, function(status) { | |||
186 | fail(); | 215 | fail(); |
187 | } | 216 | } |
188 | next(); | 217 | next(); |
189 | }, 1000); | 218 | }); |
190 | }); | 219 | }); |
191 | }, | 220 | }, |
192 | 221 | ||
@@ -203,7 +232,7 @@ page.open(url, function(status) { | |||
203 | $(".network").trigger("change"); | 232 | $(".network").trigger("change"); |
204 | }); | 233 | }); |
205 | // check the address is generated correctly | 234 | // check the address is generated correctly |
206 | setTimeout(function() { | 235 | waitForGenerate(function() { |
207 | var actual = page.evaluate(function() { | 236 | var actual = page.evaluate(function() { |
208 | return $(".address:first").text(); | 237 | return $(".address:first").text(); |
209 | }); | 238 | }); |
@@ -214,7 +243,7 @@ page.open(url, function(status) { | |||
214 | fail(); | 243 | fail(); |
215 | } | 244 | } |
216 | next(); | 245 | next(); |
217 | }, 1000); | 246 | }); |
218 | }); | 247 | }); |
219 | }, | 248 | }, |
220 | 249 | ||
@@ -231,7 +260,7 @@ page.open(url, function(status) { | |||
231 | $(".network").trigger("change"); | 260 | $(".network").trigger("change"); |
232 | }); | 261 | }); |
233 | // check the address is generated correctly | 262 | // check the address is generated correctly |
234 | setTimeout(function() { | 263 | waitForGenerate(function() { |
235 | var actual = page.evaluate(function() { | 264 | var actual = page.evaluate(function() { |
236 | return $(".address:first").text(); | 265 | return $(".address:first").text(); |
237 | }); | 266 | }); |
@@ -242,7 +271,7 @@ page.open(url, function(status) { | |||
242 | fail(); | 271 | fail(); |
243 | } | 272 | } |
244 | next(); | 273 | next(); |
245 | }, 1000); | 274 | }); |
246 | }); | 275 | }); |
247 | }, | 276 | }, |
248 | 277 | ||
@@ -259,7 +288,7 @@ page.open(url, function(status) { | |||
259 | $(".network").trigger("change"); | 288 | $(".network").trigger("change"); |
260 | }); | 289 | }); |
261 | // check the address is generated correctly | 290 | // check the address is generated correctly |
262 | setTimeout(function() { | 291 | waitForGenerate(function() { |
263 | var actual = page.evaluate(function() { | 292 | var actual = page.evaluate(function() { |
264 | return $(".address:first").text(); | 293 | return $(".address:first").text(); |
265 | }); | 294 | }); |
@@ -270,7 +299,7 @@ page.open(url, function(status) { | |||
270 | fail(); | 299 | fail(); |
271 | } | 300 | } |
272 | next(); | 301 | next(); |
273 | }, 1000); | 302 | }); |
274 | }); | 303 | }); |
275 | }, | 304 | }, |
276 | 305 | ||
@@ -287,7 +316,7 @@ page.open(url, function(status) { | |||
287 | $(".network").trigger("change"); | 316 | $(".network").trigger("change"); |
288 | }); | 317 | }); |
289 | // check the address is generated correctly | 318 | // check the address is generated correctly |
290 | setTimeout(function() { | 319 | waitForGenerate(function() { |
291 | var actual = page.evaluate(function() { | 320 | var actual = page.evaluate(function() { |
292 | return $(".address:first").text(); | 321 | return $(".address:first").text(); |
293 | }); | 322 | }); |
@@ -298,7 +327,7 @@ page.open(url, function(status) { | |||
298 | fail(); | 327 | fail(); |
299 | } | 328 | } |
300 | next(); | 329 | next(); |
301 | }, 1000); | 330 | }); |
302 | }); | 331 | }); |
303 | }, | 332 | }, |
304 | 333 | ||
@@ -315,7 +344,7 @@ page.open(url, function(status) { | |||
315 | $(".network").trigger("change"); | 344 | $(".network").trigger("change"); |
316 | }); | 345 | }); |
317 | // check the address is generated correctly | 346 | // check the address is generated correctly |
318 | setTimeout(function() { | 347 | waitForGenerate(function() { |
319 | var actual = page.evaluate(function() { | 348 | var actual = page.evaluate(function() { |
320 | return $(".address:first").text(); | 349 | return $(".address:first").text(); |
321 | }); | 350 | }); |
@@ -326,7 +355,7 @@ page.open(url, function(status) { | |||
326 | fail(); | 355 | fail(); |
327 | } | 356 | } |
328 | next(); | 357 | next(); |
329 | }, 1000); | 358 | }); |
330 | }); | 359 | }); |
331 | }, | 360 | }, |
332 | 361 | ||
@@ -343,7 +372,7 @@ page.open(url, function(status) { | |||
343 | $(".network").trigger("change"); | 372 | $(".network").trigger("change"); |
344 | }); | 373 | }); |
345 | // check the address is generated correctly | 374 | // check the address is generated correctly |
346 | setTimeout(function() { | 375 | waitForGenerate(function() { |
347 | var actual = page.evaluate(function() { | 376 | var actual = page.evaluate(function() { |
348 | return $(".address:first").text(); | 377 | return $(".address:first").text(); |
349 | }); | 378 | }); |
@@ -354,7 +383,7 @@ page.open(url, function(status) { | |||
354 | fail(); | 383 | fail(); |
355 | } | 384 | } |
356 | next(); | 385 | next(); |
357 | }, 1000); | 386 | }); |
358 | }); | 387 | }); |
359 | }, | 388 | }, |
360 | 389 | ||
@@ -371,7 +400,7 @@ page.open(url, function(status) { | |||
371 | $(".network").trigger("change"); | 400 | $(".network").trigger("change"); |
372 | }); | 401 | }); |
373 | // check the address is generated correctly | 402 | // check the address is generated correctly |
374 | setTimeout(function() { | 403 | waitForGenerate(function() { |
375 | var actual = page.evaluate(function() { | 404 | var actual = page.evaluate(function() { |
376 | return $(".address:first").text(); | 405 | return $(".address:first").text(); |
377 | }); | 406 | }); |
@@ -382,7 +411,7 @@ page.open(url, function(status) { | |||
382 | fail(); | 411 | fail(); |
383 | } | 412 | } |
384 | next(); | 413 | next(); |
385 | }, 1000); | 414 | }); |
386 | }); | 415 | }); |
387 | }, | 416 | }, |
388 | 417 | ||
@@ -399,7 +428,7 @@ page.open(url, function(status) { | |||
399 | $(".network").trigger("change"); | 428 | $(".network").trigger("change"); |
400 | }); | 429 | }); |
401 | // check the address is generated correctly | 430 | // check the address is generated correctly |
402 | setTimeout(function() { | 431 | waitForGenerate(function() { |
403 | var actual = page.evaluate(function() { | 432 | var actual = page.evaluate(function() { |
404 | return $(".address:first").text(); | 433 | return $(".address:first").text(); |
405 | }); | 434 | }); |
@@ -410,7 +439,7 @@ page.open(url, function(status) { | |||
410 | fail(); | 439 | fail(); |
411 | } | 440 | } |
412 | next(); | 441 | next(); |
413 | }, 1000); | 442 | }); |
414 | }); | 443 | }); |
415 | }, | 444 | }, |
416 | 445 | ||
@@ -424,7 +453,7 @@ page.open(url, function(status) { | |||
424 | $(".phrase").trigger("input"); | 453 | $(".phrase").trigger("input"); |
425 | }); | 454 | }); |
426 | // check the address is generated correctly | 455 | // check the address is generated correctly |
427 | setTimeout(function() { | 456 | waitForGenerate(function() { |
428 | var actual = page.evaluate(function() { | 457 | var actual = page.evaluate(function() { |
429 | return $(".seed").val(); | 458 | return $(".seed").val(); |
430 | }); | 459 | }); |
@@ -435,7 +464,7 @@ page.open(url, function(status) { | |||
435 | fail(); | 464 | fail(); |
436 | } | 465 | } |
437 | next(); | 466 | next(); |
438 | }, 1000); | 467 | }); |
439 | }); | 468 | }); |
440 | }, | 469 | }, |
441 | 470 | ||
@@ -449,7 +478,7 @@ page.open(url, function(status) { | |||
449 | $(".phrase").trigger("input"); | 478 | $(".phrase").trigger("input"); |
450 | }); | 479 | }); |
451 | // check the address is generated correctly | 480 | // check the address is generated correctly |
452 | setTimeout(function() { | 481 | waitForGenerate(function() { |
453 | var actual = page.evaluate(function() { | 482 | var actual = page.evaluate(function() { |
454 | return $(".root-key").val(); | 483 | return $(".root-key").val(); |
455 | }); | 484 | }); |
@@ -460,7 +489,7 @@ page.open(url, function(status) { | |||
460 | fail(); | 489 | fail(); |
461 | } | 490 | } |
462 | next(); | 491 | next(); |
463 | }, 1000); | 492 | }); |
464 | }); | 493 | }); |
465 | }, | 494 | }, |
466 | 495 | ||