-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-webdriver.js
209 lines (187 loc) · 5.94 KB
/
test-webdriver.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const { globSync } = require('glob');
const chrome = require('selenium-webdriver/chrome');
const firefox = require('selenium-webdriver/firefox');
const chromedriver = require('chromedriver');
const args = process.argv.slice(2);
// allow running certain browsers through command line args
// (only one browser supported, run multiple times for more browsers)
let browserArg = 'chrome';
args.forEach(arg => {
// pattern: browsers=Chrome
const parts = arg.split('=');
if (parts[0] === 'browser') {
browserArg = parts[1].toLowerCase();
}
});
/**
* Keep injecting scripts until window.mochaResults is set
*/
function collectTestResults(driver) {
// inject a script that waits half a second
return driver
.executeAsyncScript(() => {
const callback = arguments[arguments.length - 1];
setTimeout(() => {
if (!window.mocha) {
callback('mocha-missing;' + window.location.href);
}
// return the mocha results (or undefined if not finished)
callback(window.mochaResults);
}, 500);
})
.then(result => {
// If there are no results, listen a little longer
if (typeof result === 'string' && result.includes('mocha-missing')) {
throw new Error(
'Mocha does not exist in: ' +
result.split(';')[1] +
'\nIf using a frame, put the file in a subdirectory'
);
}
if (!result) {
return collectTestResults(driver);
// if there are, return them
} else {
return Promise.resolve(result);
}
});
}
/**
* Test each URL
*/
function runTestUrls(driver, isMobile, urls, errors) {
const url = urls.shift();
errors = errors || [];
return (
driver
.get(url)
// Get results
.then(() => {
return Promise.all([
driver.getCapabilities(),
collectTestResults(driver)
]);
})
// And process them
.then(promiseResults => {
const capabilities = promiseResults[0];
const result = promiseResults[1];
const browserName =
capabilities.get('browserName') +
(capabilities.get('mobileEmulationEnabled') ? '-mobile' : '');
console.log(url + ' [' + browserName + ']');
// Remember the errors
(result.reports || []).forEach(err => {
console.log(err.message);
err.url = url;
err.browser = browserName;
errors.push(err);
});
// Log the result of the page tests
console[result.failures ? 'error' : 'log'](
'passes: ' +
result.passes +
', ' +
'failures: ' +
result.failures +
', ' +
'duration: ' +
result.duration / 1000 +
's'
);
console.log();
})
.then(() => {
// Start the next job, if any
if (urls.length > 0) {
return runTestUrls(driver, isMobile, urls, errors);
} else {
driver.quit();
return Promise.resolve(errors);
}
})
);
}
/*
* Build web driver depends whether REMOTE_SELENIUM_URL is set
*/
function buildWebDriver(browser) {
let webdriver;
const mobileBrowser = browser.split('-mobile');
// fix chrome DevToolsActivePort file doesn't exist in CricleCI (as well as a
// host of other problems with starting Chrome). the only thing that seems to
// allow Chrome to start without problems consistently is using ChromeHeadless
// @see https://stackoverflow.com/questions/50642308/webdriverexception-unknown-error-devtoolsactiveport-file-doesnt-exist-while-t
if (browser === 'chrome') {
const service = new chrome.ServiceBuilder(chromedriver.path).build();
const options = new chrome.Options().addArguments([
'headless',
'--no-sandbox',
'--disable-extensions',
'--disable-dev-shm-usage'
]);
webdriver = chrome.Driver.createSession(options, service);
} else if (browser === 'firefox') {
const options = new firefox.Options().addArguments('--headless');
webdriver = firefox.Driver.createSession(options);
}
if (process.env.REMOTE_SELENIUM_URL) {
webdriver.usingServer(process.env.REMOTE_SELENIUM_URL);
}
return {
driver: webdriver,
isMobile: mobileBrowser.length > 1
};
}
function start(options) {
// yes, really, and this isn't documented anywhere either.
options.browser =
options.browser === 'edge' ? 'MicrosoftEdge' : options.browser;
const testUrls = globSync(['test/integration/full/**/*.{html,xhtml}'], {
ignore: '**/frames/**/*.{html,xhtml}'
}).map(url => {
return 'http://localhost:9876/' + url;
});
if (
(process.platform === 'win32' && options.browser === 'safari') ||
(process.platform === 'darwin' &&
['ie', 'MicrosoftEdge'].indexOf(options.browser) !== -1) ||
((process.platform === 'linux' || process.env.REMOTE_SELENIUM_URL) &&
['ie', 'MicrosoftEdge', 'safari'].indexOf(options.browser) !== -1)
) {
console.log();
console.log(
'Skipped ' + options.browser + ' as it is not supported on this platform'
);
return process.exit();
}
// try to load the browser
const webDriver = buildWebDriver(options.browser);
const driver = webDriver.driver;
const isMobile = webDriver.isMobile;
driver.manage().setTimeouts({
pageLoad: 50000,
script: !isMobile ? 60000 * 5 : 60000 * 10
});
// Test all pages
runTestUrls(driver, isMobile, testUrls)
.then(testErrors => {
// log each error and abort
testErrors.forEach(err => {
console.log();
console.log('URL: ' + err.url);
console.log('Browser: ' + err.browser);
console.log('Describe: ' + err.titles.join(' > '));
console.log('it ' + err.name);
console.log(err.stack);
console.log();
});
process.exit(testErrors.length);
// catch any potential problems
})
.catch(err => {
console.log(err);
process.exit(1);
});
}
start({ browser: browserArg });