forked from WebPlatformAutomation/www
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
302 lines (273 loc) · 8.78 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
// https://github.com/cucumber/cucumber-js/blob/d46c797aba67d36b5f446d202667d9d554b81462/docs/javascript_api.md
//
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');
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 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;
}
const { success } = await runCucumber(runConfiguration);
return success;
}
async function main() {
let argv = 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 profilePath = path.join('features', 'profiles.yml');
let profiles = yaml.load(fs.readFileSync(profilePath));
if (profiles.default) {
let defaultArgs = yargs.parse(profiles.default);
argv = { ...defaultArgs, ...argv };
}
let profile = profiles[argv.profile];
profile = { ...profile, ...argv };
// Verify some arguments
if (!/^(chromium|firefox|webkit)$/.test(profile.browser)) {
console.log(`Can't run on the browser "${profile.browser}"`);
console.log(`Use a supported browser only: chromium, firefox, and webkit.`);
exit(1);
}
global.config = {
profile,
};
if (/^(preview|live)$/.test(argv.profile)) {
let branch = getGitCurrentBranch();
global.config.profile.baseUrl = global.config.profile.baseUrl.replace('${branch}', branch);
}
let testBaseUrl = '';
if (profiles.origin) {
if (profile.testBaseUrl) {
testBaseUrl = profile.testBaseUrl;
} else if (profiles.origin.testBaseUrl) {
testBaseUrl = profiles.origin.testBaseUrl;
} else {
testBaseUrl = profiles.origin.baseUrl;
}
}
let features = profile.features && profile.features.split(',');
let pages = profile.pages;
let featureSiteDir = path.join('features', 'site');
fs.rmSync(featureSiteDir, { recursive: true, force: true });
if (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 (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);
}
}
let argvCucumber = [process.argv[0], process.argv[1]];
//for (let key of Object.keys(argv)) {
// if (['profile'].includes(key)) {
// argvCucumber.push(`--${key}`);
// argvCucumber.push(argv[key]);
// }
//}
await startCucumber({ argv: argvCucumber });
}
main();