]>
Commit | Line | Data |
---|---|---|
2af86592 SR |
1 | var Spooky = require('spooky'); |
2 | var path = require('path'); | |
3 | ||
9eca6889 | 4 | var phantom_path = require('phantomjs-prebuilt').path; |
e6af0f60 | 5 | var casper_path = path.resolve(__dirname, '../node_modules/casperjs/bin/casperjs'); |
8eb88937 | 6 | process.env.PHANTOMJS_EXECUTABLE = phantom_path; |
2af86592 SR |
7 | var casper_opts = { |
8 | child: { | |
9 | transport: 'http', | |
10 | command: casper_path | |
11 | }, | |
12 | casper: { | |
13 | logLevel: 'debug', | |
14 | verbose: true | |
15 | } | |
8eb88937 | 16 | }; |
2af86592 | 17 | |
07f514d8 SR |
18 | var provide_emitter = function(file_paths, debug_port) { |
19 | if (debug_port) { | |
20 | casper_opts.child['remote-debugger-port'] = debug_port; | |
21 | var debug_url = ('https://localhost:' + debug_port + | |
22 | '/webkit/inspector/inspector.html?page='); | |
23 | console.info('[remote-debugger] Navigate to ' + debug_url + '1 and ' + | |
24 | 'run `__run();` in the console to continue loading.' + | |
25 | '\n[remote-debugger] Navigate to ' + debug_url + '2 to ' + | |
26 | 'view the actual page source.\n' + | |
27 | '[remote-debugger] Use the `debugger;` statement to ' + | |
28 | 'trigger an initial breakpoint.'); | |
29 | } | |
30 | ||
2af86592 SR |
31 | var spooky = new Spooky(casper_opts, function(err) { |
32 | if (err) { | |
33 | if (err.stack) console.warn(err.stack); | |
34 | else console.warn(err); | |
35 | return; | |
36 | } | |
37 | spooky.start('about:blank'); | |
38 | ||
39 | file_paths.forEach(function(file_path, path_ind) { | |
40 | spooky.thenOpen('file://'+file_path); | |
9b731d3a SR |
41 | spooky.waitFor(function() { |
42 | return this.getGlobal('__mocha_done') === true; | |
43 | }, | |
44 | [{ path_ind: path_ind }, function() { | |
2af86592 SR |
45 | var res_json = { |
46 | file_ind: path_ind | |
8eb88937 | 47 | }; |
2af86592 | 48 | |
8eb88937 SR |
49 | res_json.num_tests = this.evaluate(function() { return document.querySelectorAll('li.test').length; }); |
50 | res_json.num_passes = this.evaluate(function() { return document.querySelectorAll('li.test.pass').length; }); | |
51 | res_json.num_fails = this.evaluate(function() { return document.querySelectorAll('li.test.fail').length; }); | |
52 | res_json.num_slow = this.evaluate(function() { return document.querySelectorAll('li.test.pass:not(.fast):not(.pending)').length; }); | |
53 | res_json.num_skipped = this.evaluate(function () { return document.querySelectorAll('li.test.pending').length; }); | |
54 | res_json.duration = this.evaluate(function() { return document.querySelector('li.duration em').textContent; }); | |
2af86592 SR |
55 | |
56 | res_json.suites = this.evaluate(function() { | |
57 | var traverse_node = function(elem) { | |
8eb88937 | 58 | var res; |
2af86592 | 59 | if (elem.classList.contains('suite')) { |
8eb88937 | 60 | res = { |
2af86592 SR |
61 | type: 'suite', |
62 | name: elem.querySelector('h1').textContent, | |
63 | has_subfailures: elem.querySelectorAll('li.test.fail').length > 0, | |
8eb88937 | 64 | }; |
2af86592 SR |
65 | |
66 | var child_elems = elem.querySelector('ul').children; | |
67 | res.children = Array.prototype.map.call(child_elems, traverse_node); | |
68 | return res; | |
69 | } | |
70 | else { | |
71 | var h2_content = elem.querySelector('h2').childNodes; | |
8eb88937 | 72 | res = { |
2af86592 SR |
73 | type: 'test', |
74 | text: h2_content[0].textContent, | |
8eb88937 | 75 | }; |
2af86592 SR |
76 | |
77 | if (elem.classList.contains('pass')) { | |
78 | res.pass = true; | |
8eb88937 SR |
79 | if (elem.classList.contains('pending')) { |
80 | res.slow = false; | |
81 | res.skipped = true; | |
82 | } | |
83 | else { | |
84 | res.slow = !elem.classList.contains('fast'); | |
85 | res.skipped = false; | |
86 | res.duration = h2_content[1].textContent; | |
87 | } | |
2af86592 SR |
88 | } |
89 | else { | |
90 | res.error = elem.querySelector('pre.error').textContent; | |
91 | } | |
92 | ||
93 | return res; | |
94 | } | |
8eb88937 | 95 | }; |
2af86592 SR |
96 | var top_suites = document.querySelectorAll('#mocha-report > li.suite'); |
97 | return Array.prototype.map.call(top_suites, traverse_node); | |
98 | }); | |
99 | ||
8eb88937 | 100 | res_json.replay = this.evaluate(function() { return document.querySelector('a.replay').textContent; }); |
2af86592 SR |
101 | |
102 | this.emit('test_ready', res_json); | |
103 | }]); | |
104 | }); | |
105 | spooky.run(); | |
106 | }); | |
8eb88937 | 107 | |
2af86592 | 108 | return spooky; |
8eb88937 | 109 | }; |
2af86592 SR |
110 | |
111 | module.exports = { | |
112 | provide_emitter: provide_emitter, | |
113 | name: 'SpookyJS (CapserJS on PhantomJS)' | |
8eb88937 | 114 | }; |