forked from WebPlatformAutomation/Platform-UI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
348 lines (312 loc) · 9.81 KB
/
run.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
const yargs = require('yargs');
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const fetch = require('node-fetch');
const urlJoin = require('url-join');
const convert = require('xml-js');
const parser = require('node-html-parser');
const { spawnSync } = require('child_process');
const { loadConfiguration, runCucumber } = require('@cucumber/cucumber/api');
const { ArgvParser } = require('@cucumber/cucumber/lib/configuration/index');
const { exit } = require('process');
function getProfile(args) {
let profilePath = path.join(args._[0], 'profiles.yml');
if (!fs.existsSync(profilePath)) {
console.log(`The profile config "${profilePath}" doesn't exit.`);
exit(1);
}
let profiles = yaml.load(fs.readFileSync(profilePath));
if (profiles.default) {
let defaultArgs = yargs.parse(profiles.default);
args = { ...defaultArgs, ...args };
}
let profile = profiles[args.profile];
return { ...profile, ...args };
}
function verifyBrowser(profile) {
// Verify some arguments
const supportedBrowsers = [
'chromium',
'firefox',
'webkit',
'chrome',
'chrome-beta',
'chrome-dev',
'msedge',
'msedge-beta',
'msedge-dev'
];
if (!supportedBrowsers.includes(profile.browser)) {
console.log(`The browser "${profile.browser}" is not supported.`);
console.log(`Use a supported browser: ${supportedBrowsers.join(', ')}`);
exit(1);
}
}
async function readRobotsTxt(baseUrl) {
let url = new URL(baseUrl);
let robotsTxtUrl = urlJoin(url.origin, 'robots.txt');
let res = await fetch(robotsTxtUrl);
let body = await res.text();
return body;
}
async function getSiteUrls(baseUrl) {
let text = await readRobotsTxt(baseUrl);
let lines = text.split('\n');
let sitemaps = lines.filter(x => x.trim().startsWith('sitemap:'));
sitemaps = sitemaps.map(x => x.split('sitemap:')[1].trim());
let links = [];
while (sitemaps.length > 0) {
let url = sitemaps.shift();
let res = await fetch(url);
let body = await res.text();
let data = JSON.parse(convert.xml2json(body, { compact: true, spaces: 2 }));
if ('sitemapindex' in data) {
let innerSitemaps = Array.isArray(data.sitemapindex.sitemap)
? data.sitemapindex.sitemap
: [data.sitemapindex.sitemap];
innerSitemaps = innerSitemaps.map(x => x.loc._text);
sitemaps.push(...innerSitemaps);
}
if ('urlset' in data) {
let urls = data.urlset.url.map(x => ({
url: x.loc._text,
lastmod: x.lastmod._text
}));
links.push(...urls);
console.log(`${url} has ${urls.length} links.`);
}
}
console.log(`There are ${links.length} links.`);
return links;
}
function findTestPages(links) {
let linkSet = new Set(links.map(x => x.url));
let testPages = [];
for (let link of links) {
let urlObj = new URL(link.url);
let testUrl = link.url;
if (urlObj.pathname === '/') {
testUrl += 'index-test';
} else {
testUrl += '-test';
}
if (linkSet.has(link.url) && linkSet.has(testUrl)) {
testPages.push({ url: link.url, test: testUrl });
linkSet.delete(testUrl);
}
}
return testPages;
}
function findFeaturePages(testBaseUrl, testFeatures) {
let url = new URL(testBaseUrl);
let featurePages = [];
for (let ftr of testFeatures) {
featurePages.push({
name: ftr,
test: urlJoin(url.origin, `${ftr}-feature`)
});
}
return featurePages;
}
async function getScenarios(testUrl) {
let res = await fetch(testUrl);
let body = await res.text();
let root = parser.parse(body);
let elems = root.querySelectorAll('div.test');
let scenarios = [];
for (let elem of elems) {
let lines = elem.text.split('\n');
lines = lines.map(x => x.trim());
lines = lines.filter(x => x.length > 0);
lines = lines.map(x => {
if (/^(Scenario|@|Examples)/i.test(x)) {
return ` ${x}`;
} else if (/^(Given|\|)/i.test(x)) {
return ` ${x}`;
} else if (/^(When|Then)/i.test(x)) {
return ` ${x}`;
} else if (/^(And|But)/i.test(x)) {
return ` ${x}`;
} else {
return x;
}
});
scenarios.push(lines.join('\n'));
}
return scenarios;
}
async function createFeature(testPage) {
let scenarios = await getScenarios(testPage.test);
let cucumber =
`Feature: Page function tests\n` +
` Page under test: ${testPage.url}\n` +
` Test scenarios: ${testPage.test}\n\n`;
for (let scenario of scenarios) {
cucumber += scenario + '\n\n';
}
return cucumber;
}
async function writeFeatureFileForPage(featureSiteDir, testPage) {
let feature = await createFeature(testPage);
let url = new URL(testPage.url);
let pathname = url.pathname === '/' ? '/index' : url.pathname;
let featureFilePath = path.join(featureSiteDir, `${pathname}.feature`);
let dirPath = path.dirname(featureFilePath);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
fs.writeFileSync(featureFilePath, feature);
return featureFilePath;
}
async function writeFeatureFileForFeature(featureSiteDir, feature) {
let scenarios = await getScenarios(feature.test);
let cucumber =
`Feature: Feature tests\n` + ` Feature under test: ${feature.name}\n\n`;
for (let scenario of scenarios) {
cucumber += scenario + '\n\n';
}
let featureFilePath = path.join(featureSiteDir, `${feature.name}.feature`);
let dirPath = path.dirname(featureFilePath);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
fs.writeFileSync(featureFilePath, cucumber);
return featureFilePath;
}
function getGitCurrentBranch() {
let res = spawnSync('git', ['branch', '--show-current'], {
shell: true
});
return res.stdout.toString().trim();
}
async function prepareFeatureFiles(profile) {
let testBaseUrl = profile.testBaseurl || profile.baseUrl;
let features = profile.features && profile.features.split(',');
let pages = profile.pages;
let featureSiteDir = path.join(profile.site, 'features', 'site');
fs.rmSync(featureSiteDir, { recursive: true, force: true });
if (profile.features) {
global.features = {};
let testFeatures = findFeaturePages(testBaseUrl, features);
for (let testFeature of testFeatures) {
let featureFile = await writeFeatureFileForFeature(
featureSiteDir,
testFeature
);
global.features[featureFile] = testFeature;
console.log(featureFile);
}
} else if (profile.pages) {
let testPages = [];
if (pages === true) {
let links = await getSiteUrls(testBaseUrl);
testPages = findTestPages(links);
} else {
pages = pages.split(',');
let origin = new URL(testBaseUrl).origin;
for (let page of pages) {
testPages.push({
url: urlJoin(origin, page),
test: urlJoin(origin, page + '-test')
});
}
}
global.features = {};
for (let testPage of testPages) {
let urlObj = new URL(testPage.url);
testPage.url = new URL(
urlObj.pathname,
global.config.profile.baseUrl
).toString();
let featureFile = await writeFeatureFileForPage(featureSiteDir, testPage);
global.features[featureFile] = testPage;
console.log(featureFile);
}
}
}
async function startCucumber({ argv, userConfig }) {
const { options, configuration } = ArgvParser.parse(argv);
const { useConfiguration, runConfiguration } = await loadConfiguration({
file: options.config,
profiles: options.profile,
provided: configuration
});
if (global.features) {
runConfiguration.sources.paths = Object.keys(global.features);
}
if (global.config.profile.tags) {
runConfiguration.sources.tagExpression = `${global.config.profile.tags} and not @wip`;
} else {
runConfiguration.sources.tagExpression = 'not @wip';
}
let site = global.config.profile.site;
runConfiguration.formats.stdout = '@serenity-js/cucumber';
runConfiguration.sources.paths = [`${site}/**/*.{feature,feature.md}`];
if (global.features) {
runConfiguration.sources.paths = [`${site}/features/site/**/*.feature`];
}
runConfiguration.support.requireModules = ['@babel/register'];
runConfiguration.support.requirePaths = [
'src/serenity/*.js',
'common/**/*.js',
`${site}/**/*.js`
];
if (fs.existsSync(path.resolve(__dirname, '..', 'package.json'))) {
let packageName = require('../package.json').name;
let node_module_paths = [
`node_modules/${packageName}/lib/serenity`,
`node_modules/${packageName}/lib/common`
];
for (let p of node_module_paths) {
if (fs.existsSync(p)) {
runConfiguration.support.requirePaths.push(`${p}/**/*.js`);
}
}
}
const { success } = await runCucumber(runConfiguration);
return success;
}
async function main() {
let args = yargs
.options('profile', { alias: 'p' })
.options('features', { alias: 'f' })
.option('pages', {
alias: 'g'
})
.options('tags', { alias: 't' })
.options('browser', {
alias: 'b'
})
.options('locale', { alias: 'l' })
.options('dryRun', {
alias: 'd',
type: 'boolean',
description: 'Verify step definitions'
})
.option('headless', {
alias: 'h',
type: 'boolean',
description: 'Use headless browser'
})
.option('exit', {
description: 'Force exit code'
})
.parserConfiguration({ 'strip-aliased': true }).argv;
let profile = getProfile(args);
profile.site = args._[0];
verifyBrowser(profile);
if (/^(preview|live)$/.test(args.profile)) {
let branch = getGitCurrentBranch();
profile.baseUrl = profile.baseUrl.replace('${branch}', branch);
}
global.config = {
profile
};
if (profile.pages || profile.features) {
await prepareFeatureFiles(profile);
}
let argvCucumber = [process.argv[0], process.argv[1]];
await startCucumber({ argv: argvCucumber });
}
main();