annotate static/bootstrap/js/tests/phantom.js @ 95:ddd9f7efe80c

Fixed a bug where meta values would be duplicated during resolve.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 17 Apr 2013 20:49:30 -0700
parents a5a3d454eac9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
88
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 // Simple phantom.js integration script
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 // Adapted from Modernizr
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 function waitFor(testFx, onReady, timeOutMillis) {
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 5001 //< Default Max Timout is 5s
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 , start = new Date().getTime()
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 , condition = false
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 , interval = setInterval(function () {
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) {
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 // If not time-out yet and condition not yet fulfilled
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()) //< defensive code
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 } else {
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 if (!condition) {
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 // If condition still not fulfilled (timeout but condition is 'false')
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 console.log("'waitFor()' timeout")
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 phantom.exit(1)
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 } else {
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 // Condition fulfilled (timeout and/or condition is 'true')
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 typeof(onReady) === "string" ? eval(onReady) : onReady() //< Do what it's supposed to do once the condition is fulfilled
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 clearInterval(interval) //< Stop this interval
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 }
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 }
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 }, 100) //< repeat check every 100ms
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 }
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 if (phantom.args.length === 0 || phantom.args.length > 2) {
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 console.log('Usage: phantom.js URL')
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 phantom.exit()
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 }
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 var page = new WebPage()
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 page.onConsoleMessage = function(msg) {
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 console.log(msg)
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 };
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 page.open(phantom.args[0], function(status){
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 if (status !== "success") {
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 console.log("Unable to access network")
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 phantom.exit()
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 } else {
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 waitFor(function(){
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 return page.evaluate(function(){
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 var el = document.getElementById('qunit-testresult')
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 if (el && el.innerText.match('completed')) {
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 return true
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 }
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 return false
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 })
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 }, function(){
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 var failedNum = page.evaluate(function(){
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 var el = document.getElementById('qunit-testresult')
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 try {
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 return el.getElementsByClassName('failed')[0].innerHTML
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 } catch (e) { }
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 return 10000
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 });
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0)
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 })
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 }
a5a3d454eac9 Updated Bootstrap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 })