-
Notifications
You must be signed in to change notification settings - Fork 1
/
FunctionalTestRunner.js
136 lines (101 loc) · 3.43 KB
/
FunctionalTestRunner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*!
* Phantom Mochachino
* http://doublenegative.com/phantom-mochachino
*
* Copyright 2014 Thomas Clowes <[email protected]>
* Released under the MIT license
*/
var page = require("webpage").create();
var args = require('system').args;
var config = require('./config/phantom-mochachino-config.json');
//phantom - args immutable, so make a copy
var mochachinoArgs = args.slice();
var script = mochachinoArgs.shift();
var testFile = mochachinoArgs.shift();
var pageAddress = mochachinoArgs.shift();
var stateObj = [];
var argsToInject = mochachinoArgs;
var testStatus = 0;
if (typeof testFile === 'undefined') {
console.error("Did not specify a test file");
phantom.exit();
}
var injectTestDependencies = function() {
page.injectJs(config.paths.mocha);
page.injectJs(config.paths.mochachinoInterface);
page.injectJs(config.paths.assertionLibrary);
page.injectJs(config.paths.reporter);
page.injectJs(config.paths.helpers);
page.injectJs(config.paths.testBase + testFile);
}
page.open(config.siteUrl + pageAddress);
var pages = 0;
page.onCallback = function(data) {
if (typeof data.executedItems !== 'undefined') {
stateObj = data.executedItems;
if (typeof data.differentPath !== 'undefined') {
setTimeout(function() {
page.reload();
}, 1000);
}
} else if (typeof data.status !== 'undefined') {
testStatus = data.status;
} else if (typeof data.exit !== 'undefined') {
phantom.exit(testStatus);
} else {
data.message && console.log(data.message);
}
};
var hasNewUrl = true;
page.onUrlChanged = function(targetUrl) {
hasNewUrl = true;
};
page.onConsoleMessage = function(msg, lineNum, sourceId) {
if (config.debug) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
}
};
page.onResourceRequested = function(requestData, networkRequest) {
};
page.onResourceError = function(resourceError) {
if (config.debug) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
}
};
page.onResourceReceived = function(response) {
if (config.debug) {
console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response));
}
};
page.onResourceTimeout = function(request) {
if (config.debug) {
console.log('Response (#' + request.id + '): ' + JSON.stringify(request));
}
};
page.onLoadStarted = function(status) {
};
page.onLoadFinished = function(status) {
if (hasNewUrl) {
pages++;
if (pages > 1) {
console.log('//////////////////////////////////');
console.log('///////// CHANGING PAGES /////////');
console.log('//////////////////////////////////');
}
if (status !== 'success') {
console.error("Failed to open", page.frameUrl);
phantom.exit();
}
page.injectJs("mochachino.js");
page.evaluate(function(stateObj, argsToInject) {
window.mochachino.setup(stateObj);
window.mochachino.injectArgs(argsToInject);
}, stateObj, argsToInject);
injectTestDependencies();
page.evaluate(function() {
window.mocha.run();
});
hasNewUrl = false;
}
};