webship-js 2.0.10 → 2.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.circleci/config.yml +10 -1
- package/.drone.yml +7 -7
- package/.github/ISSUE_TEMPLATE/issue.md +37 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +24 -0
- package/.github/workflows/github-actions.yml +101 -4
- package/.gitlab-ci.yml +9 -4
- package/.harness/webship-js-pipeline.yml +5 -6
- package/.semaphore/semaphore.yml +6 -9
- package/.teamcity/settings.kts +7 -6
- package/.travis.yml +6 -1
- package/AGENTS.md +1 -1
- package/CLAUDE.md +69 -24
- package/Jenkinsfile +5 -3
- package/README.md +13 -1
- package/azure-pipelines.yml +17 -8
- package/bamboo-specs/bamboo.yml +9 -15
- package/bin/generate-reports.js +51 -7
- package/bin/init-webship.js +127 -23
- package/bitbucket-pipelines.yml +5 -1
- package/buildspec.yml +6 -7
- package/cloudbuild.yaml +28 -12
- package/codefresh.yml +6 -6
- package/cucumber.js +46 -11
- package/docs/00-quick-start.md +38 -7
- package/docs/02-bbr-smart-waits.md +21 -10
- package/docs/03-selector-registry.md +24 -0
- package/docs/04-step-reference.md +53 -27
- package/docs/05-web-first-assertions.md +6 -0
- package/docs/07-auth-state.md +1 -1
- package/docs/08-clock-mocking.md +21 -2
- package/docs/09-api-testing.md +40 -2
- package/docs/10-accessibility.md +2 -0
- package/docs/11-debugging.md +24 -1
- package/docs/12-ai-agent-guide.md +8 -1
- package/docs/13-faq.md +75 -1
- package/docs/14-recipes-cookbook.md +2 -0
- package/docs/15-tag-conventions.md +5 -1
- package/docs/16-ci-cd.md +161 -124
- package/docs/README.md +5 -5
- package/docs/advanced-screenshots.md +3 -1
- package/docs/api-step-definitions.md +16 -2
- package/docs/global-settings.md +70 -18
- package/docs/install-webship-js/ddev-webship-js.md +3 -0
- package/docs/install-webship-js.md +16 -2
- package/docs/step-definitions/then-i-should-see.md +14 -1
- package/docs/step-definitions/then-item-checkbox.md +2 -0
- package/docs/step-definitions/when-i-fill.md +4 -0
- package/docs/step-definitions/when-i-select-option-select-list.md +2 -0
- package/examples/a11y-legacy-widget.html +34 -0
- package/examples/auth-state.html +42 -0
- package/examples/choice-controls.html +29 -0
- package/examples/clock.html +33 -0
- package/examples/datetime-fields.html +52 -0
- package/examples/downloads/webship-archive.zip +0 -0
- package/examples/downloads/webship-notes.txt +4 -0
- package/examples/downloads/webship-report.csv +4 -0
- package/examples/downloads.html +16 -0
- package/examples/element-selector-hint.html +16 -0
- package/examples/element-viewport.html +42 -0
- package/examples/field-states.html +27 -0
- package/examples/field-types.html +98 -0
- package/examples/fill-in-empty.html +26 -0
- package/examples/fill-unreachable.html +27 -0
- package/examples/iframe-inner.html +20 -0
- package/examples/iframe-nested.html +16 -0
- package/examples/iframe-newsletter.html +29 -0
- package/examples/iframe-order.html +26 -0
- package/examples/iframe-outer.html +15 -0
- package/examples/iframe.html +21 -0
- package/examples/javascript-errors.html +35 -0
- package/examples/links.html +1 -0
- package/examples/metatags.html +18 -0
- package/examples/mouse-and-pointer.html +83 -0
- package/examples/multi-value-fields.html +54 -0
- package/examples/native-dialogs.html +8 -0
- package/examples/network.html +83 -0
- package/examples/registered-names.html +15 -0
- package/examples/scroll-horizontal.html +50 -0
- package/examples/select-by-label.html +33 -0
- package/examples/selectors-position.html +29 -0
- package/examples/settle-budget.html +21 -0
- package/examples/sorted-tables.html +38 -0
- package/examples/storage.html +57 -0
- package/examples/text-matching.html +48 -0
- package/examples/text-order.html +25 -0
- package/examples/touch.html +22 -0
- package/examples/viewport-size.html +24 -0
- package/examples/viewport-top.html +36 -0
- package/examples/wait-conditions.html +69 -0
- package/examples/web-first.html +99 -0
- package/package.json +24 -2
- package/playwright.config.ts +3 -1
- package/tests/assets/catalog.xml +19 -0
- package/tests/assets/feed-namespaced.xml +13 -0
- package/tests/assets/sitemap.xml +14 -0
- package/tests/assets/webship-site-expected.yaml +21 -0
- package/tests/assets/webship-site.schema.json +46 -0
- package/tests/assets/webship-site.yaml +21 -0
- package/tests/check-api-launch-url.js +106 -0
- package/tests/check-assets.js +103 -0
- package/tests/check-attachments.js +105 -0
- package/tests/check-breakpoints.js +117 -0
- package/tests/check-clock.js +143 -0
- package/tests/check-config.js +90 -0
- package/tests/check-context.js +215 -0
- package/tests/check-dry-run.js +104 -0
- package/tests/check-examples.js +241 -0
- package/tests/check-exit-report.js +141 -0
- package/tests/check-javascript-errors.js +105 -0
- package/tests/check-must-fail.js +85 -0
- package/tests/check-paths.js +82 -0
- package/tests/check-report.js +119 -0
- package/tests/check-scaffold.js +246 -0
- package/tests/check-screenshots.js +117 -0
- package/tests/check-settle-verbs.js +137 -0
- package/tests/check-time-limit.js +104 -0
- package/tests/check-video.js +156 -0
- package/tests/check-world.js +167 -0
- package/tests/features/a11y.feature +18 -0
- package/tests/features/api-base-url.feature +25 -0
- package/tests/features/api-form-data.feature +18 -0
- package/tests/features/api-json-paths.feature +24 -0
- package/tests/features/api-response-contains.feature +21 -0
- package/tests/features/api-response-header-presence.feature +17 -0
- package/tests/features/api-response-header.feature +17 -0
- package/tests/features/api-saved-placeholders.feature +37 -0
- package/tests/features/api-step-definitions-individual.feature +1 -1
- package/tests/features/api-tables.feature +40 -0
- package/tests/features/auth-state.feature +32 -0
- package/tests/features/breakpoints.feature +25 -0
- package/tests/features/choice-controls.feature +35 -0
- package/tests/features/clock.feature +27 -0
- package/tests/features/complete-api-testing-examples.feature +1 -1
- package/tests/features/cookie.feature +9 -0
- package/tests/features/datetime-field-lookup.feature +29 -0
- package/tests/features/dialog.feature +27 -0
- package/tests/features/element-selector-hint.feature +14 -0
- package/tests/features/element.feature +47 -0
- package/tests/features/field-state.feature +21 -0
- package/tests/features/field.feature +104 -0
- package/tests/features/file-download.feature +64 -0
- package/tests/features/fill-in-empty-value.feature +41 -0
- package/tests/features/fill-reachable.feature +13 -0
- package/tests/features/iframe-nested.feature +43 -0
- package/tests/features/iframe.feature +83 -0
- package/tests/features/javascript-tolerated-errors.feature +20 -0
- package/tests/features/javascript.feature +47 -0
- package/tests/features/link.feature +11 -0
- package/tests/features/metatag.feature +38 -0
- package/tests/features/modal-open.feature +13 -0
- package/tests/features/mouse-and-pointer.feature +86 -0
- package/tests/features/multi-value-field-lookup.feature +30 -0
- package/tests/features/network-mocking.feature +80 -0
- package/tests/features/registered-names.feature +14 -0
- package/tests/features/responsive.feature +27 -0
- package/tests/features/scroll-horizontal.feature +55 -0
- package/tests/features/select-by-label.feature +31 -0
- package/tests/features/selector-files.feature +14 -0
- package/tests/features/selectors-position.feature +41 -0
- package/tests/features/selectors-table-header.feature +35 -0
- package/tests/features/settle-budget.feature +11 -0
- package/tests/features/sorted-tables.feature +19 -0
- package/tests/features/storage-quoted-values.feature +26 -0
- package/tests/features/storage.feature +98 -0
- package/tests/features/test--then--i-see-locator.feature +13 -0
- package/tests/features/test--then--i-should--see-text-in-element.feature +9 -1
- package/tests/features/test--then--the-field-should-contain-value.feature +8 -0
- package/tests/features/text-matching.feature +38 -0
- package/tests/features/text-order.feature +14 -0
- package/tests/features/touch.feature +21 -0
- package/tests/features/video.feature +1 -0
- package/tests/features/viewport-top.feature +13 -0
- package/tests/features/wait.feature +89 -0
- package/tests/features/web-first.feature +100 -0
- package/tests/features/xml-namespaces.feature +34 -0
- package/tests/features/xml.feature +96 -0
- package/tests/features/yaml.feature +75 -0
- package/tests/must-fail/api-json-paths.feature +21 -0
- package/tests/must-fail/api-response-contains.feature +23 -0
- package/tests/must-fail/api-response-header-presence.feature +17 -0
- package/tests/must-fail/api-response-header.feature +21 -0
- package/tests/must-fail/api-without-base-url.feature +11 -0
- package/tests/must-fail/assertions.feature +96 -0
- package/tests/must-fail/breakpoints.feature +15 -0
- package/tests/must-fail/choice-controls.feature +25 -0
- package/tests/must-fail/clock.feature +11 -0
- package/tests/must-fail/datetime-field.feature +10 -0
- package/tests/must-fail/element-selector-hint.feature +17 -0
- package/tests/must-fail/field-state.feature +20 -0
- package/tests/must-fail/fill-field.feature +30 -0
- package/tests/must-fail/iframe-text.feature +17 -0
- package/tests/must-fail/modal-open.feature +26 -0
- package/tests/must-fail/multi-value-field.feature +18 -0
- package/tests/must-fail/registered-names.feature +25 -0
- package/tests/must-fail/select-list.feature +15 -0
- package/tests/must-fail/selector-files.feature +10 -0
- package/tests/must-fail/sorted-tables.feature +20 -0
- package/tests/must-fail/storage-before-a-page.feature +13 -0
- package/tests/must-fail/text-matching.feature +16 -0
- package/tests/must-fail/text-order.feature +15 -0
- package/tests/must-fail/viewport-top.feature +20 -0
- package/tests/must-fail/waits.feature +51 -0
- package/tests/must-fail/yaml-numbers.feature +48 -0
- package/tests/reports/README.md +2 -2
- package/tests/step-definitions/a11y.steps.js +36 -23
- package/tests/step-definitions/action.steps.js +22 -8
- package/tests/step-definitions/api.steps.js +349 -67
- package/tests/step-definitions/assertion.steps.js +327 -63
- package/tests/step-definitions/auth.steps.js +15 -18
- package/tests/step-definitions/clock.steps.js +22 -0
- package/tests/step-definitions/debug.steps.js +8 -0
- package/tests/step-definitions/dialog.steps.js +9 -2
- package/tests/step-definitions/element.steps.js +135 -18
- package/tests/step-definitions/field.steps.js +523 -80
- package/tests/step-definitions/file-download.steps.js +74 -10
- package/tests/step-definitions/form.steps.js +76 -29
- package/tests/step-definitions/iframe.steps.js +89 -29
- package/tests/step-definitions/input.steps.js +26 -7
- package/tests/step-definitions/javascript.steps.js +64 -22
- package/tests/step-definitions/modal.steps.js +79 -13
- package/tests/step-definitions/navigation.steps.js +14 -0
- package/tests/step-definitions/network.steps.js +15 -5
- package/tests/step-definitions/response.steps.js +6 -4
- package/tests/step-definitions/responsive.steps.js +26 -33
- package/tests/step-definitions/screenshot.steps.js +26 -9
- package/tests/step-definitions/scroll.steps.js +20 -0
- package/tests/step-definitions/selectors.steps.js +104 -51
- package/tests/step-definitions/storage.steps.js +41 -16
- package/tests/step-definitions/table.steps.js +81 -6
- package/tests/step-definitions/video.steps.js +52 -55
- package/tests/step-definitions/wait.steps.js +209 -54
- package/tests/step-definitions/web-first.steps.js +69 -35
- package/tests/step-definitions/webship.js +807 -128
- package/tests/step-definitions/xml.steps.js +124 -28
- package/tests/step-definitions/yaml.steps.js +90 -46
- package/.scratch/commit.txt +0 -14
- package/.scratch/docs.py +0 -42
- package/.scratch/echo-server.js +0 -8
- package/.scratch/echo.js +0 -35
- package/.scratch/echo.pid +0 -1
- package/.scratch/features/cookies.feature +0 -22
- package/.scratch/http.pid +0 -1
- package/.scratch/https.pid +0 -1
- package/.scratch/issue.md +0 -49
- package/.scratch/one.js +0 -2
- package/.scratch/one.json +0 -280
- package/.scratch/patch.py +0 -163
- package/.scratch/pr.md +0 -30
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// Checks the HTML report bin/generate-reports.js writes, from a small
|
|
5
|
+
// cucumber JSON, without a browser:
|
|
6
|
+
//
|
|
7
|
+
// 1. it links no icon of a third-party site,
|
|
8
|
+
// 2. "App Version" is the version of the project the report is made for,
|
|
9
|
+
// and is left out when the project has no package.json,
|
|
10
|
+
// 3. "Executed" is "Local" on a tester's machine and "Remote" on a CI
|
|
11
|
+
// runner, unless it is given.
|
|
12
|
+
//
|
|
13
|
+
// Usage: node tests/check-report.js (npm run test:report)
|
|
14
|
+
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
const os = require('os');
|
|
17
|
+
const path = require('path');
|
|
18
|
+
const { spawnSync } = require('child_process');
|
|
19
|
+
|
|
20
|
+
const GENERATE = path.resolve(__dirname, '..', 'bin', 'generate-reports.js');
|
|
21
|
+
const problems = [];
|
|
22
|
+
|
|
23
|
+
const SAMPLE = [{
|
|
24
|
+
keyword: 'Feature',
|
|
25
|
+
name: 'A sample feature',
|
|
26
|
+
line: 1,
|
|
27
|
+
id: 'a-sample-feature',
|
|
28
|
+
uri: 'tests/features/sample.feature',
|
|
29
|
+
tags: [],
|
|
30
|
+
elements: [{
|
|
31
|
+
keyword: 'Scenario',
|
|
32
|
+
name: 'A sample scenario',
|
|
33
|
+
line: 3,
|
|
34
|
+
id: 'a-sample-feature;a-sample-scenario',
|
|
35
|
+
type: 'scenario',
|
|
36
|
+
tags: [],
|
|
37
|
+
steps: [{
|
|
38
|
+
keyword: 'Given ',
|
|
39
|
+
name: 'I am on the homepage',
|
|
40
|
+
line: 4,
|
|
41
|
+
arguments: [],
|
|
42
|
+
match: { location: 'tests/step-definitions/navigation.steps.js:44' },
|
|
43
|
+
result: { status: 'passed', duration: 1000000 },
|
|
44
|
+
}],
|
|
45
|
+
}],
|
|
46
|
+
}];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Generate a report in a new project folder and return its HTML.
|
|
50
|
+
*
|
|
51
|
+
* @param {string} tmp - Parent folder.
|
|
52
|
+
* @param {string} name - Name of the project folder.
|
|
53
|
+
* @param {Object|null} pkg - Its package.json, or null for none.
|
|
54
|
+
* @param {Object} env - Variables set (a null value unsets one).
|
|
55
|
+
* @param {string[]} [args] - Extra arguments of generate-reports.
|
|
56
|
+
* @returns {string}
|
|
57
|
+
*/
|
|
58
|
+
function report(tmp, name, pkg, env, args) {
|
|
59
|
+
const project = path.join(tmp, name);
|
|
60
|
+
fs.mkdirSync(path.join(project, 'tests', 'reports'), { recursive: true });
|
|
61
|
+
if (pkg) fs.writeFileSync(path.join(project, 'package.json'), JSON.stringify(pkg));
|
|
62
|
+
fs.writeFileSync(path.join(project, 'tests', 'reports', 'cucumber_report.json'), JSON.stringify(SAMPLE));
|
|
63
|
+
const environment = Object.assign({}, process.env);
|
|
64
|
+
for (const key of ['CI', 'WEBSHIP_REPORT_APP_VERSION', 'WEBSHIP_REPORT_EXECUTED', 'WEBSHIP_REPORT_ARGS']) delete environment[key];
|
|
65
|
+
for (const [key, value] of Object.entries(env)) {
|
|
66
|
+
if (value === null) delete environment[key];
|
|
67
|
+
else environment[key] = value;
|
|
68
|
+
}
|
|
69
|
+
const run = spawnSync(process.execPath, [GENERATE].concat(args || []), { cwd: project, encoding: 'utf8', env: environment });
|
|
70
|
+
const out = path.join(project, 'tests', 'reports', 'cucumber_report.html');
|
|
71
|
+
if (run.status !== 0 || !fs.existsSync(out)) {
|
|
72
|
+
problems.push(`${name}: generate-reports exited with ${run.status} and wrote ${fs.existsSync(out) ? 'a' : 'no'} report\n${run.stderr}`);
|
|
73
|
+
return '';
|
|
74
|
+
}
|
|
75
|
+
return fs.readFileSync(out, 'utf8');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The value the Metadata block of a report shows for a key, or null. */
|
|
79
|
+
function metadata(html, key) {
|
|
80
|
+
const text = html.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ');
|
|
81
|
+
const match = text.match(new RegExp(key + ':\\s*(\\S+)'));
|
|
82
|
+
return match ? match[1] : null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'webship-js-report-'));
|
|
86
|
+
try {
|
|
87
|
+
const local = report(tmp, 'local', { name: 'local', version: '3.4.5' }, {});
|
|
88
|
+
if (local) {
|
|
89
|
+
const icons = local.match(/<link\b[^>]*\brel=["']?(?:shortcut )?icon["']?[^>]*>/gi) || [];
|
|
90
|
+
for (const icon of icons) {
|
|
91
|
+
if (/href=["']?(https?:)?\/\//i.test(icon)) problems.push(`the report links an icon of another site: ${icon}`);
|
|
92
|
+
}
|
|
93
|
+
if (/lambdatest/i.test(local)) problems.push('the report names a third-party site (lambdatest)');
|
|
94
|
+
if (!/A sample scenario/.test(local)) problems.push('the report does not show the scenario of the JSON');
|
|
95
|
+
if (metadata(local, 'App Version') !== '3.4.5') problems.push(`App Version is "${metadata(local, 'App Version')}", the project's package.json says 3.4.5`);
|
|
96
|
+
if (metadata(local, 'Executed') !== 'Local') problems.push(`Executed is "${metadata(local, 'Executed')}" for a run without CI set; expected Local`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const ci = report(tmp, 'ci', { name: 'ci', version: '1.0.0' }, { CI: 'true' });
|
|
100
|
+
if (ci && metadata(ci, 'Executed') !== 'Remote') problems.push(`Executed is "${metadata(ci, 'Executed')}" with CI set; expected Remote`);
|
|
101
|
+
|
|
102
|
+
const given = report(tmp, 'given', { name: 'given', version: '1.0.0' }, { WEBSHIP_REPORT_APP_VERSION: '9.9.9' }, ['--executed', 'Staging']);
|
|
103
|
+
if (given) {
|
|
104
|
+
if (metadata(given, 'App Version') !== '9.9.9') problems.push(`WEBSHIP_REPORT_APP_VERSION=9.9.9 gave App Version "${metadata(given, 'App Version')}"`);
|
|
105
|
+
if (metadata(given, 'Executed') !== 'Staging') problems.push(`--executed Staging gave Executed "${metadata(given, 'Executed')}"`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const bare = report(tmp, 'bare', null, {});
|
|
109
|
+
if (bare && metadata(bare, 'App Version') !== null) problems.push(`a project without package.json got App Version "${metadata(bare, 'App Version')}"`);
|
|
110
|
+
} finally {
|
|
111
|
+
fs.rmSync(tmp, { recursive: true, force: true });
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (problems.length) {
|
|
115
|
+
console.error(`${problems.length} problem(s) with the HTML report:`);
|
|
116
|
+
for (const p of problems) console.error(' ' + p);
|
|
117
|
+
process.exit(1);
|
|
118
|
+
}
|
|
119
|
+
console.log('The HTML report links no third-party icon; App Version and Executed say what is true for the project and the run.');
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// Checks the scaffold a new project gets (bin/init-webship.js, the script the
|
|
5
|
+
// post-install runs), the way a new user meets it:
|
|
6
|
+
//
|
|
7
|
+
// 1. `npm init -y` in an empty folder, then the scaffold: the files and the
|
|
8
|
+
// package.json scripts are there, and `npm test` is the suite, not npm's
|
|
9
|
+
// "no test specified" placeholder,
|
|
10
|
+
// 2. `npm test` in that project is green against a site (the examples/
|
|
11
|
+
// pages of this repository, served on a free port),
|
|
12
|
+
// 3. a feature named on the command line runs alone,
|
|
13
|
+
// 4. a second scaffold run changes nothing, and a `test` script the project
|
|
14
|
+
// chose itself is kept,
|
|
15
|
+
// 5. a `test` script that runs Nightwatch (a project from webship-js 1.x)
|
|
16
|
+
// is replaced by the suite and kept as `test:nightwatch`,
|
|
17
|
+
// 6. `--force` rewrites the scaffold's files but keeps the project's own
|
|
18
|
+
// steps in tests/step-definitions/custom.js.
|
|
19
|
+
//
|
|
20
|
+
// The browser install is skipped (--skip-browsers): Chromium must be in the
|
|
21
|
+
// Playwright cache already, as it is after `npx playwright install chromium`.
|
|
22
|
+
//
|
|
23
|
+
// Usage: node tests/check-scaffold.js (npm run test:scaffold)
|
|
24
|
+
|
|
25
|
+
const crypto = require('crypto');
|
|
26
|
+
const fs = require('fs');
|
|
27
|
+
const net = require('net');
|
|
28
|
+
const os = require('os');
|
|
29
|
+
const path = require('path');
|
|
30
|
+
const { spawn, spawnSync } = require('child_process');
|
|
31
|
+
|
|
32
|
+
const ROOT = path.resolve(__dirname, '..');
|
|
33
|
+
const INIT = path.join(ROOT, 'bin', 'init-webship.js');
|
|
34
|
+
const NPM = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
35
|
+
const problems = [];
|
|
36
|
+
|
|
37
|
+
const EXPECTED_FILES = [
|
|
38
|
+
'cucumber.js',
|
|
39
|
+
'playwright.config.ts',
|
|
40
|
+
'tsconfig.json',
|
|
41
|
+
'package.json',
|
|
42
|
+
'tests/features/check-homepage.feature',
|
|
43
|
+
'tests/step-definitions/custom.js',
|
|
44
|
+
'tests/reports/README.md',
|
|
45
|
+
];
|
|
46
|
+
const EXPECTED_SCRIPTS = ['test', 'test:chromium', 'test:firefox', 'test:webkit', 'test:headed', 'test:fast', 'generate-reports'];
|
|
47
|
+
|
|
48
|
+
function scaffold(dir) {
|
|
49
|
+
return spawnSync(process.execPath, [INIT, '--cwd', dir, '--skip-browsers'], { encoding: 'utf8' });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function hashes(dir) {
|
|
53
|
+
const out = {};
|
|
54
|
+
for (const file of EXPECTED_FILES) {
|
|
55
|
+
const full = path.join(dir, file);
|
|
56
|
+
out[file] = fs.existsSync(full) ? crypto.createHash('sha1').update(fs.readFileSync(full)).digest('hex') : null;
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// The project uses this checkout as its webship-js and shares its
|
|
62
|
+
// dependencies, so nothing is downloaded.
|
|
63
|
+
function linkDependencies(dir) {
|
|
64
|
+
const target = path.join(dir, 'node_modules');
|
|
65
|
+
fs.mkdirSync(target, { recursive: true });
|
|
66
|
+
for (const entry of fs.readdirSync(path.join(ROOT, 'node_modules'))) {
|
|
67
|
+
fs.symlinkSync(path.join(ROOT, 'node_modules', entry), path.join(target, entry));
|
|
68
|
+
}
|
|
69
|
+
fs.symlinkSync(ROOT, path.join(target, 'webship-js'));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function freePort() {
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
const server = net.createServer();
|
|
75
|
+
server.on('error', reject);
|
|
76
|
+
server.listen(0, '127.0.0.1', () => {
|
|
77
|
+
const { port } = server.address();
|
|
78
|
+
server.close(() => resolve(port));
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function waitForPort(port, ms) {
|
|
84
|
+
const deadline = Date.now() + ms;
|
|
85
|
+
return new Promise((resolve) => {
|
|
86
|
+
const attempt = () => {
|
|
87
|
+
const socket = net.connect(port, '127.0.0.1');
|
|
88
|
+
socket.on('connect', () => { socket.destroy(); resolve(true); });
|
|
89
|
+
socket.on('error', () => {
|
|
90
|
+
socket.destroy();
|
|
91
|
+
if (Date.now() > deadline) resolve(false);
|
|
92
|
+
else setTimeout(attempt, 200);
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
attempt();
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function main() {
|
|
100
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'webship-js-scaffold-'));
|
|
101
|
+
let server = null;
|
|
102
|
+
try {
|
|
103
|
+
// 1. A new project: npm init -y, then the scaffold.
|
|
104
|
+
const project = path.join(tmp, 'new-project');
|
|
105
|
+
fs.mkdirSync(project);
|
|
106
|
+
const init = spawnSync(NPM, ['init', '-y'], { cwd: project, encoding: 'utf8', shell: process.platform === 'win32' });
|
|
107
|
+
if (init.status !== 0) throw new Error(`npm init -y failed: ${init.stderr}`);
|
|
108
|
+
const placeholder = JSON.parse(fs.readFileSync(path.join(project, 'package.json'), 'utf8')).scripts.test;
|
|
109
|
+
|
|
110
|
+
const first = scaffold(project);
|
|
111
|
+
if (first.status !== 0) problems.push(`the scaffold exited with ${first.status}: ${first.stderr}`);
|
|
112
|
+
for (const file of EXPECTED_FILES) {
|
|
113
|
+
if (!fs.existsSync(path.join(project, file))) problems.push(`the scaffold did not write ${file}`);
|
|
114
|
+
}
|
|
115
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(project, 'package.json'), 'utf8'));
|
|
116
|
+
for (const name of EXPECTED_SCRIPTS) {
|
|
117
|
+
if (!pkg.scripts || !pkg.scripts[name]) problems.push(`package.json has no "${name}" script`);
|
|
118
|
+
}
|
|
119
|
+
if (pkg.scripts && pkg.scripts.test === placeholder) {
|
|
120
|
+
problems.push(`"npm test" is still the placeholder of npm init: ${placeholder}`);
|
|
121
|
+
}
|
|
122
|
+
if (!pkg.dependencies || !pkg.dependencies['webship-js']) problems.push('package.json does not depend on webship-js');
|
|
123
|
+
|
|
124
|
+
// 2. npm test in the new project, against the examples/ pages.
|
|
125
|
+
linkDependencies(project);
|
|
126
|
+
const port = await freePort();
|
|
127
|
+
server = spawn(process.execPath, [path.join(ROOT, 'node_modules', 'http-server', 'bin', 'http-server'), path.join(ROOT, 'examples'), '-p', String(port), '-a', '127.0.0.1', '-s'], { stdio: 'ignore' });
|
|
128
|
+
if (!await waitForPort(port, 15000)) throw new Error(`the examples server did not start on port ${port}`);
|
|
129
|
+
const run = spawnSync(NPM, ['test'], {
|
|
130
|
+
cwd: project,
|
|
131
|
+
encoding: 'utf8',
|
|
132
|
+
shell: process.platform === 'win32',
|
|
133
|
+
env: Object.assign({}, process.env, { LAUNCH_URL: `http://127.0.0.1:${port}`, FORCE_COLOR: '0', SLOW_MO: '0', WEBSHIP_REPORT_DISABLE: '1' }),
|
|
134
|
+
});
|
|
135
|
+
const report = path.join(project, 'tests', 'reports', 'cucumber_report.json');
|
|
136
|
+
let scenarios = 0;
|
|
137
|
+
let failed = 0;
|
|
138
|
+
if (fs.existsSync(report)) {
|
|
139
|
+
for (const feature of JSON.parse(fs.readFileSync(report, 'utf8'))) {
|
|
140
|
+
for (const scenario of feature.elements || []) {
|
|
141
|
+
scenarios++;
|
|
142
|
+
if ((scenario.steps || []).some((step) => step.result.status !== 'passed')) failed++;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (run.status !== 0 || !scenarios || failed) {
|
|
147
|
+
problems.push(`"npm test" in the new project: exit ${run.status}, ${scenarios} scenario(s), ${failed} not passed\n${(run.stdout || '').split('\n').slice(-25).join('\n')}\n${run.stderr || ''}`);
|
|
148
|
+
} else {
|
|
149
|
+
console.log(` npm test in the new project: ${scenarios} scenario passed.`);
|
|
150
|
+
}
|
|
151
|
+
if (/colorsEnabled/.test((run.stdout || '') + (run.stderr || ''))) {
|
|
152
|
+
problems.push('the scaffolded cucumber.js makes cucumber-js print a deprecation warning (colorsEnabled)');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// A feature named on the command line runs alone, beside a second one.
|
|
156
|
+
const extra = path.join(project, 'tests', 'features', 'second.feature');
|
|
157
|
+
fs.writeFileSync(extra, 'Feature: Second\n\n Scenario: Second\n Given I am on the homepage\n');
|
|
158
|
+
const namedReport = path.join(tmp, 'named.json');
|
|
159
|
+
const named = spawnSync(process.execPath, [path.join(ROOT, 'node_modules', '@cucumber', 'cucumber', 'bin', 'cucumber.js'), '--dry-run', 'tests/features/second.feature', '--format', `json:${namedReport}`], {
|
|
160
|
+
cwd: project,
|
|
161
|
+
encoding: 'utf8',
|
|
162
|
+
env: Object.assign({}, process.env, { FORCE_COLOR: '0', WEBSHIP_REPORT_DISABLE: '1' }),
|
|
163
|
+
});
|
|
164
|
+
const namedScenarios = fs.existsSync(namedReport)
|
|
165
|
+
? JSON.parse(fs.readFileSync(namedReport, 'utf8')).reduce((n, feature) => n + (feature.elements || []).length, 0)
|
|
166
|
+
: -1;
|
|
167
|
+
if (namedScenarios !== 1) {
|
|
168
|
+
problems.push(`naming one feature on the command line in the new project selected ${namedScenarios} scenario(s), not 1\n${named.stderr || ''}`);
|
|
169
|
+
}
|
|
170
|
+
fs.rmSync(extra, { force: true });
|
|
171
|
+
|
|
172
|
+
// 3. A second run changes nothing.
|
|
173
|
+
fs.rmSync(path.join(project, 'node_modules'), { recursive: true, force: true });
|
|
174
|
+
const before = hashes(project);
|
|
175
|
+
const second = scaffold(project);
|
|
176
|
+
if (second.status !== 0) problems.push(`the second scaffold run exited with ${second.status}`);
|
|
177
|
+
const after = hashes(project);
|
|
178
|
+
for (const file of EXPECTED_FILES) {
|
|
179
|
+
if (before[file] !== after[file]) problems.push(`a second scaffold run changed ${file}`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// A test script the project chose is kept.
|
|
183
|
+
const own = path.join(tmp, 'own-test-script');
|
|
184
|
+
fs.mkdirSync(own);
|
|
185
|
+
fs.writeFileSync(path.join(own, 'package.json'), JSON.stringify({ name: 'own-test-script', version: '1.0.0', scripts: { test: 'node my-own-tests.js' } }, null, 2));
|
|
186
|
+
scaffold(own);
|
|
187
|
+
const ownPkg = JSON.parse(fs.readFileSync(path.join(own, 'package.json'), 'utf8'));
|
|
188
|
+
if (ownPkg.scripts.test !== 'node my-own-tests.js') problems.push(`the scaffold replaced a test script of the project: ${ownPkg.scripts.test}`);
|
|
189
|
+
if (!ownPkg.scripts['test:chromium']) problems.push('the scaffold did not add test:chromium beside an existing test script');
|
|
190
|
+
|
|
191
|
+
// A project from webship-js 1.x: its test script runs Nightwatch.
|
|
192
|
+
const legacy = path.join(tmp, 'from-1x');
|
|
193
|
+
fs.mkdirSync(legacy);
|
|
194
|
+
const nightwatch = 'nightwatch --format @cucumber/pretty-formatter --format json:./tests/reports/cucumber-report.json';
|
|
195
|
+
fs.writeFileSync(path.join(legacy, 'package.json'), JSON.stringify({ name: 'from-1x', version: '1.0.0', scripts: { test: nightwatch } }, null, 2));
|
|
196
|
+
const legacyRun = scaffold(legacy);
|
|
197
|
+
const legacyPkg = JSON.parse(fs.readFileSync(path.join(legacy, 'package.json'), 'utf8'));
|
|
198
|
+
if (legacyPkg.scripts.test !== 'cucumber-js --config cucumber.js') {
|
|
199
|
+
problems.push(`the scaffold kept a Nightwatch test script from 1.x: "npm test" is still ${legacyPkg.scripts.test}`);
|
|
200
|
+
}
|
|
201
|
+
if (legacyPkg.scripts['test:nightwatch'] !== nightwatch) {
|
|
202
|
+
problems.push(`the scaffold did not keep the old Nightwatch command as test:nightwatch: ${legacyPkg.scripts['test:nightwatch']}`);
|
|
203
|
+
}
|
|
204
|
+
if (legacyPkg.scripts.test !== nightwatch && !/nightwatch/i.test(legacyRun.stdout || '')) {
|
|
205
|
+
problems.push('the scaffold replaced the Nightwatch test script without saying so');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// --force keeps the project's own steps.
|
|
209
|
+
const customFile = path.join(project, 'tests', 'step-definitions', 'custom.js');
|
|
210
|
+
const ownSteps = fs.readFileSync(customFile, 'utf8') + "\nGiven('a step of the project', function () {});\n";
|
|
211
|
+
fs.writeFileSync(customFile, ownSteps);
|
|
212
|
+
fs.writeFileSync(path.join(project, 'playwright.config.ts'), '// changed by the project\n');
|
|
213
|
+
const forced = spawnSync(process.execPath, [INIT, '--cwd', project, '--skip-browsers', '--force'], { encoding: 'utf8' });
|
|
214
|
+
if (forced.status !== 0) problems.push(`the scaffold with --force exited with ${forced.status}`);
|
|
215
|
+
if (fs.readFileSync(customFile, 'utf8') !== ownSteps) {
|
|
216
|
+
problems.push('the scaffold with --force overwrote tests/step-definitions/custom.js, the steps of the project');
|
|
217
|
+
}
|
|
218
|
+
if (fs.readFileSync(path.join(project, 'playwright.config.ts'), 'utf8') === '// changed by the project\n') {
|
|
219
|
+
problems.push('the scaffold with --force did not rewrite playwright.config.ts');
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// No package.json at all: the scaffold writes one.
|
|
223
|
+
const bare = path.join(tmp, 'no-package-json');
|
|
224
|
+
fs.mkdirSync(bare);
|
|
225
|
+
scaffold(bare);
|
|
226
|
+
const barePkgPath = path.join(bare, 'package.json');
|
|
227
|
+
if (!fs.existsSync(barePkgPath) || !JSON.parse(fs.readFileSync(barePkgPath, 'utf8')).scripts.test) {
|
|
228
|
+
problems.push('without a package.json the scaffold did not write one with a test script');
|
|
229
|
+
}
|
|
230
|
+
} finally {
|
|
231
|
+
if (server) server.kill();
|
|
232
|
+
fs.rmSync(tmp, { recursive: true, force: true });
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (problems.length) {
|
|
236
|
+
console.error(`\n${problems.length} problem(s) with the scaffold:`);
|
|
237
|
+
for (const p of problems) console.error(' ' + p);
|
|
238
|
+
process.exit(1);
|
|
239
|
+
}
|
|
240
|
+
console.log('The scaffold gives a new project its files and scripts, and its first "npm test" is green.');
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
main().catch((error) => {
|
|
244
|
+
console.error(error.stack || String(error));
|
|
245
|
+
process.exit(1);
|
|
246
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// Runs three scenarios with the failure screenshots on and checks which of
|
|
5
|
+
// them leave a "failed_" screenshot: the one whose step fails does; the one
|
|
6
|
+
// that a step skips on purpose (the step returns 'skipped', the way a suite
|
|
7
|
+
// leaves out a scenario that does not apply to the site) and the one that
|
|
8
|
+
// stops at a pending step do not. The failure screenshot is also attached to
|
|
9
|
+
// the failed step in the JSON report (an image/png embedding), so the HTML
|
|
10
|
+
// report shows it; the other two steps carry none.
|
|
11
|
+
//
|
|
12
|
+
// The scenarios never navigate, so no server is needed.
|
|
13
|
+
//
|
|
14
|
+
// Usage: node tests/check-screenshots.js (npm run test:screenshots)
|
|
15
|
+
|
|
16
|
+
const fs = require('fs');
|
|
17
|
+
const os = require('os');
|
|
18
|
+
const path = require('path');
|
|
19
|
+
const { spawnSync } = require('child_process');
|
|
20
|
+
|
|
21
|
+
const ROOT = path.resolve(__dirname, '..');
|
|
22
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'webship-js-screenshots-'));
|
|
23
|
+
const shots = path.join(tmp, 'screenshots');
|
|
24
|
+
const report = path.join(tmp, 'outcomes.json');
|
|
25
|
+
const config = path.join(tmp, 'cucumber.js');
|
|
26
|
+
const steps = path.join(tmp, 'outcomes.steps.js');
|
|
27
|
+
const feature = path.join(tmp, 'outcomes.feature');
|
|
28
|
+
|
|
29
|
+
const cucumber = path.join(ROOT, 'node_modules', '@cucumber', 'cucumber');
|
|
30
|
+
fs.writeFileSync(steps, [
|
|
31
|
+
`const { Given, Then } = require(${JSON.stringify(cucumber)});`,
|
|
32
|
+
"Given('this scenario does not apply here', function () { return 'skipped'; });",
|
|
33
|
+
"Given('this step is not written yet', function () { return 'pending'; });",
|
|
34
|
+
"Then('this step fails on purpose', function () { throw new Error('Failing on purpose.'); });",
|
|
35
|
+
'',
|
|
36
|
+
].join('\n'));
|
|
37
|
+
fs.writeFileSync(feature, [
|
|
38
|
+
'Feature: Outcomes and failure screenshots',
|
|
39
|
+
'',
|
|
40
|
+
' Scenario: A step skips the scenario on purpose',
|
|
41
|
+
' Given this scenario does not apply here',
|
|
42
|
+
'',
|
|
43
|
+
' Scenario: A step is pending',
|
|
44
|
+
' Given this step is not written yet',
|
|
45
|
+
'',
|
|
46
|
+
' Scenario: A step fails',
|
|
47
|
+
' Then this step fails on purpose',
|
|
48
|
+
'',
|
|
49
|
+
' Scenario: Named screenshots',
|
|
50
|
+
' Then I save screenshot with name "homepage.png"',
|
|
51
|
+
' And I save fullscreen screenshot with name "full-page.JPG"',
|
|
52
|
+
' And I save screenshot with name "plain-name"',
|
|
53
|
+
'',
|
|
54
|
+
].join('\n'));
|
|
55
|
+
const lines = { skipped: 4, pending: 7, failed: 10 };
|
|
56
|
+
|
|
57
|
+
const base = require(path.join(ROOT, 'cucumber.js')).default;
|
|
58
|
+
const world = JSON.parse(JSON.stringify(base.worldParameters));
|
|
59
|
+
world.screenshot = Object.assign({}, world.screenshot, { dir: shots, onFailed: true, onEveryStep: false, purge: false });
|
|
60
|
+
fs.writeFileSync(config, 'module.exports = { default: ' + JSON.stringify({
|
|
61
|
+
requireModule: base.requireModule,
|
|
62
|
+
require: base.require.concat([steps]),
|
|
63
|
+
paths: [feature],
|
|
64
|
+
format: [`json:${report}`],
|
|
65
|
+
worldParameters: world,
|
|
66
|
+
}) + ' };');
|
|
67
|
+
|
|
68
|
+
spawnSync(process.execPath, [path.join(cucumber, 'bin', 'cucumber.js'), '--config', path.relative(ROOT, config)], {
|
|
69
|
+
cwd: ROOT,
|
|
70
|
+
stdio: ['ignore', 'ignore', 'inherit'],
|
|
71
|
+
env: Object.assign({ SLOW_MO: '0' }, process.env, { WEBSHIP_REPORT_DISABLE: '1', WEBSHIP_VIDEO: 'off', WEBSHIP_JS_ERROR_MODE: 'off' }),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const problems = [];
|
|
75
|
+
if (!fs.existsSync(report) || !fs.readFileSync(report, 'utf8').trim()) {
|
|
76
|
+
problems.push('The run wrote no report: cucumber-js did not start.');
|
|
77
|
+
} else {
|
|
78
|
+
const statuses = {};
|
|
79
|
+
const images = {};
|
|
80
|
+
for (const f of JSON.parse(fs.readFileSync(report, 'utf8'))) {
|
|
81
|
+
for (const scenario of f.elements || []) {
|
|
82
|
+
for (const step of (scenario.steps || []).filter((s) => !s.hidden)) {
|
|
83
|
+
statuses[step.line] = step.result && step.result.status;
|
|
84
|
+
images[step.line] = (step.embeddings || []).filter((e) => e.mime_type === 'image/png' && e.data).length;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
for (const [outcome, line] of Object.entries(lines)) {
|
|
89
|
+
if (statuses[line] !== outcome) problems.push(`The step on line ${line} ended "${statuses[line]}", not "${outcome}": the check itself is broken.`);
|
|
90
|
+
}
|
|
91
|
+
const files = fs.existsSync(shots) ? fs.readdirSync(shots).filter((name) => name.startsWith('failed_')) : [];
|
|
92
|
+
const forLine = (line) => files.filter((name) => new RegExp(`\\.feature_${line}\\.(png|html)$`).test(name));
|
|
93
|
+
if (!forLine(lines.failed).some((name) => name.endsWith('.png'))) {
|
|
94
|
+
problems.push(`The failed step (line ${lines.failed}) left no failure screenshot; files: ${files.join(', ') || 'none'}.`);
|
|
95
|
+
}
|
|
96
|
+
for (const outcome of ['skipped', 'pending']) {
|
|
97
|
+
const found = forLine(lines[outcome]);
|
|
98
|
+
if (found.length) problems.push(`The ${outcome} step (line ${lines[outcome]}) left failure screenshots: ${found.join(', ')}.`);
|
|
99
|
+
if (images[lines[outcome]]) problems.push(`The ${outcome} step (line ${lines[outcome]}) carries ${images[lines[outcome]]} screenshot(s) in the JSON report.`);
|
|
100
|
+
}
|
|
101
|
+
// A name that ends with the extension of a file the step writes keeps
|
|
102
|
+
// that one extension: "homepage.png" is homepage.png, not homepage.png.png.
|
|
103
|
+
const saved = fs.existsSync(shots) ? fs.readdirSync(shots) : [];
|
|
104
|
+
for (const name of ['homepage.png', 'homepage.html', 'full-page.png', 'full-page.html', 'plain-name.png', 'plain-name.html']) {
|
|
105
|
+
if (!saved.includes(name)) problems.push(`The named screenshot ${name} was not written; files: ${saved.filter((f) => !f.startsWith('failed_')).join(', ') || 'none'}.`);
|
|
106
|
+
}
|
|
107
|
+
if (images[lines.failed] !== 1) {
|
|
108
|
+
problems.push(`The failed step (line ${lines.failed}) carries ${images[lines.failed] || 0} screenshot(s) in the JSON report, not 1: no report can show it.`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
fs.rmSync(tmp, { recursive: true, force: true });
|
|
113
|
+
if (problems.length) {
|
|
114
|
+
console.error('Failure screenshots:\n - ' + problems.join('\n - '));
|
|
115
|
+
process.exit(1);
|
|
116
|
+
}
|
|
117
|
+
console.log('Failure screenshots: the failed step left one, attached to it in the JSON report; the skipped and the pending step left none. Named screenshots keep the one extension their name gives.');
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// Runs a project's own steps (written here, the way a project writes them)
|
|
5
|
+
// whose words carry a page-changing verb — add, log out, enter, open,
|
|
6
|
+
// delete, save — and checks that the settle after the step waited for the
|
|
7
|
+
// page: the next step reads the element once and finds the change that
|
|
8
|
+
// arrives 600 ms after the action. A built-in assertion whose quoted text
|
|
9
|
+
// holds such a word ("Then I should see "Delete"") gets no settle.
|
|
10
|
+
//
|
|
11
|
+
// The check serves its own page on a free local port, so no other server is
|
|
12
|
+
// needed.
|
|
13
|
+
//
|
|
14
|
+
// Usage: node tests/check-settle-verbs.js (npm run test:settle-verbs)
|
|
15
|
+
|
|
16
|
+
const fs = require('fs');
|
|
17
|
+
const os = require('os');
|
|
18
|
+
const path = require('path');
|
|
19
|
+
const http = require('http');
|
|
20
|
+
const { spawn } = require('child_process');
|
|
21
|
+
|
|
22
|
+
const ROOT = path.resolve(__dirname, '..');
|
|
23
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'webship-js-settle-verbs-'));
|
|
24
|
+
const report = path.join(tmp, 'settle.json');
|
|
25
|
+
const config = path.join(tmp, 'cucumber.js');
|
|
26
|
+
const steps = path.join(tmp, 'project.steps.js');
|
|
27
|
+
const feature = path.join(tmp, 'settle.feature');
|
|
28
|
+
const cucumber = path.join(ROOT, 'node_modules', '@cucumber', 'cucumber');
|
|
29
|
+
|
|
30
|
+
const PAGE = `<!doctype html><html><head><title>Shop</title></head><body>
|
|
31
|
+
<h1>Shop</h1>
|
|
32
|
+
<button id="act" type="button">Act</button>
|
|
33
|
+
<p id="status">Nothing yet</p>
|
|
34
|
+
<p>Delete</p>
|
|
35
|
+
<script>
|
|
36
|
+
document.getElementById('act').addEventListener('click', function () {
|
|
37
|
+
setTimeout(function () { document.getElementById('status').textContent = 'Done'; }, 600);
|
|
38
|
+
});
|
|
39
|
+
</script></body></html>`;
|
|
40
|
+
|
|
41
|
+
// A project's own steps: each one clicks the button the way a custom step
|
|
42
|
+
// would, without waiting for anything.
|
|
43
|
+
const phrases = [
|
|
44
|
+
'I add "Laptop" to the cart',
|
|
45
|
+
'I log out of the shop',
|
|
46
|
+
'I enter the 2FA code "123456"',
|
|
47
|
+
'I open the account menu',
|
|
48
|
+
'I delete the draft',
|
|
49
|
+
'I save the order',
|
|
50
|
+
];
|
|
51
|
+
fs.writeFileSync(steps, [
|
|
52
|
+
`const { When } = require(${JSON.stringify(cucumber)});`,
|
|
53
|
+
...phrases.map((p) => {
|
|
54
|
+
const args = (p.match(/"[^"]*"/g) || []).map((_, i) => `value${i}`).join(', ');
|
|
55
|
+
return `When(${JSON.stringify(p.replace(/"[^"]*"/g, '{string}'))}, async function (${args}) { await this.page.click('#act'); });`;
|
|
56
|
+
}),
|
|
57
|
+
'',
|
|
58
|
+
].join('\n'));
|
|
59
|
+
fs.writeFileSync(feature, [
|
|
60
|
+
'Feature: A project step that changes the page is followed by the settle',
|
|
61
|
+
'',
|
|
62
|
+
...phrases.flatMap((p) => [
|
|
63
|
+
` Scenario: ${p}`,
|
|
64
|
+
' Given I am on "/"',
|
|
65
|
+
` When ${p}`,
|
|
66
|
+
' Then I should see "Done" in the "#status" element',
|
|
67
|
+
'',
|
|
68
|
+
]),
|
|
69
|
+
' Scenario: A wait for AJAX in lower case',
|
|
70
|
+
' Given I am on "/"',
|
|
71
|
+
' When I wait for ajax to finish',
|
|
72
|
+
' Then I should see "Shop"',
|
|
73
|
+
'',
|
|
74
|
+
].join('\n'));
|
|
75
|
+
|
|
76
|
+
const server = http.createServer((req, res) => {
|
|
77
|
+
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
78
|
+
res.end(PAGE);
|
|
79
|
+
});
|
|
80
|
+
server.listen(0, '127.0.0.1', () => {
|
|
81
|
+
const base = `http://127.0.0.1:${server.address().port}`;
|
|
82
|
+
const defaults = require(path.join(ROOT, 'cucumber.js')).default;
|
|
83
|
+
const world = JSON.parse(JSON.stringify(defaults.worldParameters));
|
|
84
|
+
world.launchUrl = base;
|
|
85
|
+
fs.writeFileSync(config, 'module.exports = { default: ' + JSON.stringify({
|
|
86
|
+
requireModule: defaults.requireModule,
|
|
87
|
+
require: defaults.require.concat([steps]),
|
|
88
|
+
paths: [feature],
|
|
89
|
+
format: [`json:${report}`],
|
|
90
|
+
worldParameters: world,
|
|
91
|
+
}) + ' };');
|
|
92
|
+
const env = Object.assign({ SLOW_MO: '0' }, process.env, {
|
|
93
|
+
LAUNCH_URL: base,
|
|
94
|
+
WEBSHIP_REPORT_DISABLE: '1',
|
|
95
|
+
WEBSHIP_VIDEO: 'off',
|
|
96
|
+
WEBSHIP_JS_ERROR_MODE: 'off',
|
|
97
|
+
WEBSHIP_SCREENSHOT_ON_FAILED: 'false',
|
|
98
|
+
});
|
|
99
|
+
delete env.WEBSHIP_AUTO_SETTLE;
|
|
100
|
+
const run = spawn(process.execPath, [path.join(cucumber, 'bin', 'cucumber.js'), '--config', path.relative(ROOT, config)], {
|
|
101
|
+
cwd: ROOT,
|
|
102
|
+
stdio: ['ignore', 'ignore', 'pipe'],
|
|
103
|
+
env,
|
|
104
|
+
});
|
|
105
|
+
let errors = '';
|
|
106
|
+
run.stderr.on('data', (chunk) => { errors += chunk; });
|
|
107
|
+
const timer = setTimeout(() => run.kill('SIGKILL'), 180000);
|
|
108
|
+
run.on('exit', () => {
|
|
109
|
+
clearTimeout(timer);
|
|
110
|
+
server.close();
|
|
111
|
+
finish(errors);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
function finish(errors) {
|
|
116
|
+
const strip = (text) => String(text || '').replace(/\u001b\[[0-9;]*m/g, '');
|
|
117
|
+
const problems = [];
|
|
118
|
+
let count = 0;
|
|
119
|
+
if (!fs.existsSync(report) || !fs.readFileSync(report, 'utf8').trim()) {
|
|
120
|
+
problems.push(`The run wrote no report.\n${strip(errors).slice(-1500)}`);
|
|
121
|
+
} else {
|
|
122
|
+
for (const f of JSON.parse(fs.readFileSync(report, 'utf8'))) {
|
|
123
|
+
for (const scenario of f.elements || []) {
|
|
124
|
+
count += 1;
|
|
125
|
+
const failed = (scenario.steps || []).find((step) => step.result && step.result.status !== 'passed');
|
|
126
|
+
if (failed) problems.push(`"${scenario.name}": ${failed.hidden ? 'a hook' : `"${failed.keyword}${failed.name}"`} ${failed.result.status}: ${strip(failed.result.error_message).split('\n')[0]}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (count !== phrases.length + 1) problems.push(`${count} scenarios ran, not ${phrases.length + 1}.`);
|
|
130
|
+
}
|
|
131
|
+
fs.rmSync(tmp, { recursive: true, force: true });
|
|
132
|
+
if (problems.length) {
|
|
133
|
+
console.error('The settle after a project step:\n - ' + problems.join('\n - '));
|
|
134
|
+
process.exit(1);
|
|
135
|
+
}
|
|
136
|
+
console.log('The settle after a project step: every page-changing verb waits for the page; the AJAX wait reads in lower case.');
|
|
137
|
+
}
|