webship-js 2.0.1 → 2.0.2
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/bin/generate-reports.js +191 -0
- package/bin/init-webship.js +343 -0
- package/bin/postinstall.js +8 -74
- package/cucumber.js +59 -1
- package/examples/diffy/baseline.html +54 -0
- package/examples/diffy/changed.html +63 -0
- package/examples/diffy/index.html +52 -0
- package/examples/index.html +6 -0
- package/examples/test--when--i-additionally-select.html +27 -0
- package/package.json +10 -4
- package/tests/features/diffy/test--diffy-01-example-demo.feature +22 -0
- package/tests/features/diffy/test--diffy-02-all-steps.feature +52 -0
- package/tests/features/diffy/test--diffy-03-real-api.feature +21 -0
- package/tests/features/test--then--i-save-screenshot.feature +33 -0
- package/tests/features/test--then--i-see-locator.feature +320 -0
- package/tests/features/test--then--i-should-see-n-elements.feature +24 -0
- package/tests/features/test--then--print-current-url-and-response.feature +21 -0
- package/tests/features/test--when--i-additionally-select.feature +30 -0
- package/tests/features/test--when--i-follow.feature +22 -0
- package/tests/selectors/back-end-selectors.json +13 -0
- package/tests/selectors/front-end-selectors.json +16 -0
- package/tests/selectors/homepage-selectors.json +13 -0
- package/tests/step-definitions/webship-api.js +234 -41
- package/tests/step-definitions/webship-screenshot.js +427 -0
- package/tests/step-definitions/webship-selectors.js +961 -0
- package/tests/step-definitions/webship.js +829 -230
- package/tests/step-definitions-diffy/README.md +329 -0
- package/tests/step-definitions-diffy/mock-diffy-api/server.js +84 -0
- package/tests/step-definitions-diffy/webship-diffy-mock.js +31 -0
- package/tests/step-definitions-diffy/webship-diffy.js +472 -0
- package/generate-reports.js +0 -22
- package/required/cucumber.js +0 -37
- package/required/generate-reports.js +0 -22
- package/required/package.json +0 -18
- package/required/playwright.config.ts +0 -37
- package/required/tests/features/README.md +0 -1
- package/required/tests/features/check-homepage.feature +0 -8
- package/required/tests/reports/README.md +0 -1
- package/required/tests/step-definitions/README.md +0 -1
- package/required/tests/step-definitions/custom.js +0 -4
- package/required/tsconfig.json +0 -12
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const reporter = require('cucumber-html-reporter');
|
|
7
|
+
|
|
8
|
+
const THEMES = new Set(['bootstrap', 'hierarchy', 'foundation', 'simple']);
|
|
9
|
+
|
|
10
|
+
function bool(v) {
|
|
11
|
+
return v !== 'false' && v !== '0' && v !== 'no';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function capitalize(s) {
|
|
15
|
+
return s ? s.charAt(0).toUpperCase() + s.slice(1) : s;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function parseArgs(argv) {
|
|
19
|
+
const opts = {
|
|
20
|
+
metadata: {},
|
|
21
|
+
scenarioTimestamp: true,
|
|
22
|
+
reportSuiteAsScenarios: true,
|
|
23
|
+
failedSummaryReport: true,
|
|
24
|
+
launchReport: false,
|
|
25
|
+
noInlineScreenshots: false,
|
|
26
|
+
storeScreenshots: false,
|
|
27
|
+
ignoreBadJsonFile: false,
|
|
28
|
+
};
|
|
29
|
+
for (let i = 0; i < argv.length; i++) {
|
|
30
|
+
const a = argv[i];
|
|
31
|
+
const next = () => argv[++i];
|
|
32
|
+
switch (a) {
|
|
33
|
+
case '-h':
|
|
34
|
+
case '--help': opts.help = true; break;
|
|
35
|
+
case '--json': opts.jsonFile = next(); break;
|
|
36
|
+
case '--out':
|
|
37
|
+
case '--output': opts.output = next(); break;
|
|
38
|
+
case '--theme': opts.theme = next(); break;
|
|
39
|
+
case '--title': opts.brandTitle = next(); break;
|
|
40
|
+
case '--name': opts.name = next(); break;
|
|
41
|
+
case '--launch': opts.launchReport = true; break;
|
|
42
|
+
case '--layout': opts.columnLayout = Number(next()); break;
|
|
43
|
+
case '--no-scenario-timestamp': opts.scenarioTimestamp = false; break;
|
|
44
|
+
case '--no-suite-as-scenarios': opts.reportSuiteAsScenarios = false; break;
|
|
45
|
+
case '--no-failed-summary': opts.failedSummaryReport = false; break;
|
|
46
|
+
case '--no-inline-screenshots': opts.noInlineScreenshots = true; break;
|
|
47
|
+
case '--store-screenshots': opts.storeScreenshots = true; break;
|
|
48
|
+
case '--screenshots-dir': opts.screenshotsDirectory = next(); break;
|
|
49
|
+
case '--ignore-bad-json': opts.ignoreBadJsonFile = true; break;
|
|
50
|
+
case '--app-version': opts.metadata['App Version'] = next(); break;
|
|
51
|
+
case '--env': opts.metadata['Test Environment'] = next(); break;
|
|
52
|
+
case '--browser': opts.metadata['Browser'] = next(); break;
|
|
53
|
+
case '--platform': opts.metadata['Platform'] = next(); break;
|
|
54
|
+
case '--parallel': opts.metadata['Parallel'] = next(); break;
|
|
55
|
+
case '--executed': opts.metadata['Executed'] = next(); break;
|
|
56
|
+
case '--metadata': {
|
|
57
|
+
const kv = next() || '';
|
|
58
|
+
const eq = kv.indexOf('=');
|
|
59
|
+
if (eq > 0) opts.metadata[kv.slice(0, eq)] = kv.slice(eq + 1);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
default:
|
|
63
|
+
if (a.startsWith('--')) {
|
|
64
|
+
console.error(`[webship-js] Unknown option: ${a}`);
|
|
65
|
+
opts.help = true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return opts;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function printHelp() {
|
|
73
|
+
process.stdout.write(
|
|
74
|
+
[
|
|
75
|
+
'Usage: generate-reports [options]',
|
|
76
|
+
'',
|
|
77
|
+
'Generate cucumber HTML report from JSON output.',
|
|
78
|
+
'',
|
|
79
|
+
'Input / Output:',
|
|
80
|
+
' --json <path> JSON input (env WEBSHIP_REPORT_JSON,',
|
|
81
|
+
' default: tests/reports/cucumber_report.json)',
|
|
82
|
+
' --out <path> HTML output (env WEBSHIP_REPORT_OUT,',
|
|
83
|
+
' default: tests/reports/cucumber_report.html)',
|
|
84
|
+
'',
|
|
85
|
+
'Format:',
|
|
86
|
+
' --theme <name> bootstrap | hierarchy | foundation | simple',
|
|
87
|
+
' (env WEBSHIP_REPORT_THEME, default: bootstrap)',
|
|
88
|
+
' --title <s> Brand title (env WEBSHIP_REPORT_TITLE)',
|
|
89
|
+
' --name <s> Report name',
|
|
90
|
+
' --layout <1|2> Column layout (default: 1)',
|
|
91
|
+
' --launch Launch report in browser after generation',
|
|
92
|
+
' --no-scenario-timestamp Hide per-scenario timestamp',
|
|
93
|
+
' --no-suite-as-scenarios Report suites instead of scenarios',
|
|
94
|
+
' --no-failed-summary Hide failed-scenarios summary',
|
|
95
|
+
' --no-inline-screenshots Link screenshots instead of embedding',
|
|
96
|
+
' --store-screenshots Copy screenshots into report dir',
|
|
97
|
+
' --screenshots-dir <dir> Source screenshots directory',
|
|
98
|
+
' --ignore-bad-json Do not fail on malformed JSON entries',
|
|
99
|
+
'',
|
|
100
|
+
'Metadata:',
|
|
101
|
+
' --app-version <s> App Version (env WEBSHIP_REPORT_APP_VERSION)',
|
|
102
|
+
' --env <s> Test Environment (env WEBSHIP_REPORT_ENV)',
|
|
103
|
+
' --browser <s> Browser (env BROWSER)',
|
|
104
|
+
' --platform <s> Platform (default: process.platform)',
|
|
105
|
+
' --parallel <s> Parallel (default: Scenarios)',
|
|
106
|
+
' --executed <s> Executed (env WEBSHIP_REPORT_EXECUTED)',
|
|
107
|
+
' --metadata key=value Arbitrary key/value (repeatable)',
|
|
108
|
+
'',
|
|
109
|
+
' -h, --help Show this help',
|
|
110
|
+
'',
|
|
111
|
+
].join('\n')
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function run(argv) {
|
|
116
|
+
const args = parseArgs(argv);
|
|
117
|
+
if (args.help) return printHelp();
|
|
118
|
+
|
|
119
|
+
const cwd = process.cwd();
|
|
120
|
+
const jsonFile = path.resolve(
|
|
121
|
+
cwd,
|
|
122
|
+
args.jsonFile ||
|
|
123
|
+
process.env.WEBSHIP_REPORT_JSON ||
|
|
124
|
+
'tests/reports/cucumber_report.json'
|
|
125
|
+
);
|
|
126
|
+
const output = path.resolve(
|
|
127
|
+
cwd,
|
|
128
|
+
args.output ||
|
|
129
|
+
process.env.WEBSHIP_REPORT_OUT ||
|
|
130
|
+
'tests/reports/cucumber_report.html'
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const theme =
|
|
134
|
+
args.theme || process.env.WEBSHIP_REPORT_THEME || 'bootstrap';
|
|
135
|
+
if (!THEMES.has(theme)) {
|
|
136
|
+
console.error(
|
|
137
|
+
`[webship-js] Unknown theme "${theme}". Use one of: ${[...THEMES].join(', ')}`
|
|
138
|
+
);
|
|
139
|
+
process.exit(1);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const metadata = {
|
|
143
|
+
'App Version':
|
|
144
|
+
args.metadata['App Version'] ||
|
|
145
|
+
process.env.WEBSHIP_REPORT_APP_VERSION ||
|
|
146
|
+
'2.0.0',
|
|
147
|
+
'Test Environment':
|
|
148
|
+
args.metadata['Test Environment'] ||
|
|
149
|
+
process.env.WEBSHIP_REPORT_ENV ||
|
|
150
|
+
'development',
|
|
151
|
+
Browser:
|
|
152
|
+
args.metadata['Browser'] ||
|
|
153
|
+
(process.env.BROWSER ? capitalize(process.env.BROWSER) : 'Chromium'),
|
|
154
|
+
Platform: args.metadata['Platform'] || process.platform,
|
|
155
|
+
Parallel: args.metadata['Parallel'] || 'Scenarios',
|
|
156
|
+
Executed:
|
|
157
|
+
args.metadata['Executed'] ||
|
|
158
|
+
process.env.WEBSHIP_REPORT_EXECUTED ||
|
|
159
|
+
'Remote',
|
|
160
|
+
};
|
|
161
|
+
for (const [k, v] of Object.entries(args.metadata)) {
|
|
162
|
+
metadata[k] = v;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const options = {
|
|
166
|
+
theme,
|
|
167
|
+
jsonFile,
|
|
168
|
+
output,
|
|
169
|
+
reportSuiteAsScenarios: args.reportSuiteAsScenarios,
|
|
170
|
+
scenarioTimestamp: args.scenarioTimestamp,
|
|
171
|
+
launchReport: args.launchReport,
|
|
172
|
+
brandTitle:
|
|
173
|
+
args.brandTitle || process.env.WEBSHIP_REPORT_TITLE || 'Test Report',
|
|
174
|
+
name: args.name || process.env.WEBSHIP_REPORT_NAME,
|
|
175
|
+
columnLayout: args.columnLayout || 1,
|
|
176
|
+
noInlineScreenshots: args.noInlineScreenshots,
|
|
177
|
+
storeScreenshots: args.storeScreenshots,
|
|
178
|
+
screenshotsDirectory: args.screenshotsDirectory,
|
|
179
|
+
ignoreBadJsonFile: args.ignoreBadJsonFile,
|
|
180
|
+
failedSummaryReport: args.failedSummaryReport,
|
|
181
|
+
metadata,
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
reporter.generate(options);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (require.main === module) {
|
|
188
|
+
run(process.argv.slice(2));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
module.exports = { run };
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { spawnSync } = require('child_process');
|
|
8
|
+
|
|
9
|
+
const packagePath = path.join(__dirname, '..');
|
|
10
|
+
|
|
11
|
+
const CUCUMBER_JS = `module.exports = {
|
|
12
|
+
default: {
|
|
13
|
+
timeout: 30000,
|
|
14
|
+
requireModule: ['ts-node/register'],
|
|
15
|
+
require: [
|
|
16
|
+
'node_modules/webship-js/tests/step-definitions/**/*.js', // Webship-js core step definitions (auto HTML report on exit; disable: WEBSHIP_REPORT_DISABLE=1).
|
|
17
|
+
// 'node_modules/webship-js/tests/step-definitions-diffy/**/*.js', // Diffy step definitions (optional).
|
|
18
|
+
'tests/step-definitions/**/*.js', // Your custom step definitions.
|
|
19
|
+
],
|
|
20
|
+
paths: ['tests/features/**/*.feature'],
|
|
21
|
+
format: [
|
|
22
|
+
'@cucumber/pretty-formatter',
|
|
23
|
+
'json:tests/reports/cucumber_report.json',
|
|
24
|
+
],
|
|
25
|
+
formatOptions: {
|
|
26
|
+
colorsEnabled: true,
|
|
27
|
+
theme: {
|
|
28
|
+
'feature keyword': ['bold', 'blue'],
|
|
29
|
+
'feature name': ['blue', 'underline'],
|
|
30
|
+
'feature description': ['blueBright'],
|
|
31
|
+
'scenario keyword': ['bold', 'magenta'],
|
|
32
|
+
'scenario name': ['magenta', 'underline'],
|
|
33
|
+
'step keyword': ['bold', 'green'],
|
|
34
|
+
'step text': ['greenBright', 'italic'],
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
worldParameters: {
|
|
38
|
+
launchUrl: process.env.LAUNCH_URL || 'http://localhost:8080',
|
|
39
|
+
minWaitTime: {
|
|
40
|
+
page: 3000,
|
|
41
|
+
before_scenario: 0,
|
|
42
|
+
after_scenario: 0,
|
|
43
|
+
before_step: 0,
|
|
44
|
+
after_step: 0,
|
|
45
|
+
},
|
|
46
|
+
selectors: {
|
|
47
|
+
css: {},
|
|
48
|
+
xpath: {},
|
|
49
|
+
filesPath: './tests/selectors/',
|
|
50
|
+
files: [],
|
|
51
|
+
offset: 60,
|
|
52
|
+
breakpoints: {
|
|
53
|
+
xs: { width: 375, height: 667 },
|
|
54
|
+
sm: { width: 576, height: 800 },
|
|
55
|
+
md: { width: 768, height: 1024 },
|
|
56
|
+
lg: { width: 992, height: 768 },
|
|
57
|
+
xl: { width: 1200, height: 900, default: true },
|
|
58
|
+
xxl: { width: 1400, height: 900 },
|
|
59
|
+
xxxl: { width: 1920, height: 1080 },
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
screenshot: {
|
|
63
|
+
dir: './screenshots',
|
|
64
|
+
purge: false,
|
|
65
|
+
onFailed: true,
|
|
66
|
+
onEveryStep: false,
|
|
67
|
+
alwaysFullscreen: false,
|
|
68
|
+
failedPrefix: 'failed_',
|
|
69
|
+
filenamePattern: '{datetime}.{feature_file}.feature_{step_line}.{ext}',
|
|
70
|
+
filenamePatternFailed: '{failed_prefix}{datetime}.{feature_file}.feature_{step_line}.{ext}',
|
|
71
|
+
infoTypes: '',
|
|
72
|
+
},
|
|
73
|
+
// Diffy visual-regression (optional). Uncomment and set apiKey / projectId to enable.
|
|
74
|
+
// diffy: {
|
|
75
|
+
// apiKey: '', // DIFFY_API_KEY
|
|
76
|
+
// projectId: 0, // DIFFY_PROJECT_ID
|
|
77
|
+
// breakpoints: '640,1200', // DIFFY_BREAKPOINTS
|
|
78
|
+
// windowHeight: 2000, // DIFFY_WINDOW_HEIGHT
|
|
79
|
+
// screenshotsDir: '', // DIFFY_SCREENSHOTS_DIR
|
|
80
|
+
// baseUrl: 'https://app.diffy.website/api/', // DIFFY_API_BASE_URL
|
|
81
|
+
// maxWait: 30, // DIFFY_MAX_WAIT (seconds)
|
|
82
|
+
// env1Url: '', // DIFFY_ENV1_URL
|
|
83
|
+
// env2Url: '', // DIFFY_ENV2_URL
|
|
84
|
+
// },
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
`;
|
|
89
|
+
|
|
90
|
+
const PLAYWRIGHT_CONFIG_TS = `import type { LaunchOptions, BrowserContextOptions } from 'playwright';
|
|
91
|
+
|
|
92
|
+
type BrowserName = 'chromium' | 'firefox' | 'webkit';
|
|
93
|
+
|
|
94
|
+
const browser = (process.env.BROWSER || 'chromium') as BrowserName;
|
|
95
|
+
|
|
96
|
+
const chromiumArgs: string[] = [
|
|
97
|
+
'--no-sandbox',
|
|
98
|
+
'--disable-dev-shm-usage',
|
|
99
|
+
'--disable-setuid-sandbox',
|
|
100
|
+
'--disable-web-security',
|
|
101
|
+
'--ignore-certificate-errors',
|
|
102
|
+
'--disable-extensions',
|
|
103
|
+
'--incognito',
|
|
104
|
+
'--disable-infobars',
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
interface PlaywrightConfig {
|
|
108
|
+
browser: BrowserName;
|
|
109
|
+
launchOptions: LaunchOptions;
|
|
110
|
+
contextOptions: BrowserContextOptions;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const config: PlaywrightConfig = {
|
|
114
|
+
browser,
|
|
115
|
+
launchOptions: {
|
|
116
|
+
headless: true,
|
|
117
|
+
slowMo: 300,
|
|
118
|
+
args: browser === 'chromium' ? chromiumArgs : [],
|
|
119
|
+
},
|
|
120
|
+
contextOptions: {
|
|
121
|
+
viewport: { width: 1600, height: 1200 },
|
|
122
|
+
ignoreHTTPSErrors: true,
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export = config;
|
|
127
|
+
`;
|
|
128
|
+
|
|
129
|
+
const TSCONFIG_JSON = {
|
|
130
|
+
compilerOptions: {
|
|
131
|
+
target: 'ES2020',
|
|
132
|
+
module: 'CommonJS',
|
|
133
|
+
strict: true,
|
|
134
|
+
esModuleInterop: true,
|
|
135
|
+
types: ['node'],
|
|
136
|
+
},
|
|
137
|
+
'ts-node': {
|
|
138
|
+
ignore: ['node_modules/(?!webship-js/)'],
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const FEATURE_SAMPLE = `Feature: Check the home page
|
|
143
|
+
As a nonymouse user
|
|
144
|
+
I want to be able to visit the home page
|
|
145
|
+
So that I know that the site is working
|
|
146
|
+
|
|
147
|
+
Scenario: Check homepage
|
|
148
|
+
Given I am on the homepage
|
|
149
|
+
Then I should see "Love to help in speeding up the work of having Automated Functional Acceptance Testing for products to ship websites in a swift way."
|
|
150
|
+
`;
|
|
151
|
+
|
|
152
|
+
const CUSTOM_STEP_JS = `const { Given, When, Then } = require('@cucumber/cucumber');
|
|
153
|
+
const playwrightConfig = require(require('path').join(process.cwd(), 'playwright.config'));
|
|
154
|
+
|
|
155
|
+
// Add your custom step definitions here.
|
|
156
|
+
`;
|
|
157
|
+
|
|
158
|
+
const TEXT_ASSETS = {
|
|
159
|
+
'cucumber.js': CUCUMBER_JS,
|
|
160
|
+
'playwright.config.ts': PLAYWRIGHT_CONFIG_TS,
|
|
161
|
+
'tests/features/README.md': '# Features\n',
|
|
162
|
+
'tests/features/check-homepage.feature': FEATURE_SAMPLE,
|
|
163
|
+
'tests/reports/README.md': '# Reports\n',
|
|
164
|
+
'tests/step-definitions/README.md': '# Step Definitions\n',
|
|
165
|
+
'tests/step-definitions/custom.js': CUSTOM_STEP_JS,
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const TEST_SCRIPTS = {
|
|
169
|
+
test: 'cucumber-js --config cucumber.js',
|
|
170
|
+
'test:chromium': 'BROWSER=chromium cucumber-js --config cucumber.js',
|
|
171
|
+
'test:firefox': 'BROWSER=firefox cucumber-js --config cucumber.js',
|
|
172
|
+
'test:webkit': 'BROWSER=webkit cucumber-js --config cucumber.js',
|
|
173
|
+
'generate-reports': 'generate-reports',
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const DEFAULT_PKG = {
|
|
177
|
+
name: 'project-name',
|
|
178
|
+
version: '2.0.0',
|
|
179
|
+
description: '',
|
|
180
|
+
main: 'index.js',
|
|
181
|
+
author: '',
|
|
182
|
+
license: 'MIT',
|
|
183
|
+
private: false,
|
|
184
|
+
scripts: {},
|
|
185
|
+
homepage: 'https://github.com/webship/webship-js',
|
|
186
|
+
dependencies: {},
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
function parseArgs(argv) {
|
|
190
|
+
const opts = { force: false, skipBrowsers: false, help: false, cwd: null };
|
|
191
|
+
for (let i = 0; i < argv.length; i++) {
|
|
192
|
+
const a = argv[i];
|
|
193
|
+
if (a === '--force' || a === '-f') opts.force = true;
|
|
194
|
+
else if (a === '--skip-browsers') opts.skipBrowsers = true;
|
|
195
|
+
else if (a === '--help' || a === '-h') opts.help = true;
|
|
196
|
+
else if (a === '--cwd') opts.cwd = argv[++i];
|
|
197
|
+
}
|
|
198
|
+
return opts;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function printHelp() {
|
|
202
|
+
process.stdout.write(
|
|
203
|
+
[
|
|
204
|
+
'Usage: init-webship-js [options]',
|
|
205
|
+
'',
|
|
206
|
+
'Scaffold a webship-js project: cucumber.js, playwright.config.ts,',
|
|
207
|
+
'tsconfig.json, tests/ tree, and package.json scripts.',
|
|
208
|
+
'Idempotent — safe to run multiple times.',
|
|
209
|
+
'',
|
|
210
|
+
'Options:',
|
|
211
|
+
' -f, --force Overwrite existing files',
|
|
212
|
+
' --skip-browsers Skip `playwright install chromium`',
|
|
213
|
+
' --cwd <dir> Target directory (default: $INIT_CWD or cwd)',
|
|
214
|
+
' -h, --help Show this help',
|
|
215
|
+
'',
|
|
216
|
+
].join('\n')
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function writeFileIfMissing(destPath, content, { force }) {
|
|
221
|
+
if (!force && fs.existsSync(destPath)) return false;
|
|
222
|
+
fs.mkdirSync(path.dirname(destPath), { recursive: true });
|
|
223
|
+
fs.writeFileSync(destPath, content, 'utf8');
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function ensureDir(dir) {
|
|
228
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function readPkgVersion() {
|
|
232
|
+
try {
|
|
233
|
+
const pkg = JSON.parse(
|
|
234
|
+
fs.readFileSync(path.join(packagePath, 'package.json'), 'utf8')
|
|
235
|
+
);
|
|
236
|
+
const major = String(pkg.version).split('.')[0] || '2';
|
|
237
|
+
return `~${major}.0.0`;
|
|
238
|
+
} catch {
|
|
239
|
+
return '~2.0.0';
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function writeTextAssets(projectPath, opts) {
|
|
244
|
+
const results = [];
|
|
245
|
+
for (const [rel, content] of Object.entries(TEXT_ASSETS)) {
|
|
246
|
+
const dest = path.join(projectPath, rel);
|
|
247
|
+
const written = writeFileIfMissing(dest, content, opts);
|
|
248
|
+
results.push({ file: rel, written });
|
|
249
|
+
}
|
|
250
|
+
return results;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function writeTsconfig(projectPath, opts) {
|
|
254
|
+
const dest = path.join(projectPath, 'tsconfig.json');
|
|
255
|
+
if (!opts.force && fs.existsSync(dest)) return false;
|
|
256
|
+
fs.writeFileSync(dest, JSON.stringify(TSCONFIG_JSON, null, 2) + '\n', 'utf8');
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function ensurePackageJson(projectPath, opts) {
|
|
261
|
+
const pkgPath = path.join(projectPath, 'package.json');
|
|
262
|
+
const existed = fs.existsSync(pkgPath);
|
|
263
|
+
|
|
264
|
+
let pkg;
|
|
265
|
+
if (existed) {
|
|
266
|
+
pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
267
|
+
} else {
|
|
268
|
+
pkg = { ...DEFAULT_PKG };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
pkg.scripts = pkg.scripts || {};
|
|
272
|
+
for (const [name, cmd] of Object.entries(TEST_SCRIPTS)) {
|
|
273
|
+
if (opts.force || !pkg.scripts[name]) {
|
|
274
|
+
pkg.scripts[name] = cmd;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
pkg.dependencies = pkg.dependencies || {};
|
|
279
|
+
if (opts.force || !pkg.dependencies['webship-js']) {
|
|
280
|
+
pkg.dependencies['webship-js'] = readPkgVersion();
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n', 'utf8');
|
|
284
|
+
return { created: !existed };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function installBrowsers(projectPath) {
|
|
288
|
+
console.log('\n [webship-js] Installing Playwright browsers...\n');
|
|
289
|
+
const res = spawnSync(
|
|
290
|
+
'npx',
|
|
291
|
+
['playwright', 'install', '--with-deps', 'chromium'],
|
|
292
|
+
{ cwd: projectPath, stdio: 'inherit', shell: true }
|
|
293
|
+
);
|
|
294
|
+
return res.status === 0;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function run(argv) {
|
|
298
|
+
const opts = parseArgs(argv);
|
|
299
|
+
if (opts.help) return printHelp();
|
|
300
|
+
|
|
301
|
+
const projectPath = path.resolve(
|
|
302
|
+
opts.cwd || process.env.INIT_CWD || process.cwd()
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
if (
|
|
306
|
+
projectPath === packagePath ||
|
|
307
|
+
projectPath.startsWith(path.join(packagePath, 'node_modules'))
|
|
308
|
+
) {
|
|
309
|
+
console.log(' [webship-js] Skipping init inside webship-js itself.');
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
console.log(`\n [webship-js] Initializing project at ${projectPath}\n`);
|
|
314
|
+
|
|
315
|
+
ensureDir(projectPath);
|
|
316
|
+
ensureDir(path.join(projectPath, 'tests', 'features'));
|
|
317
|
+
ensureDir(path.join(projectPath, 'tests', 'reports'));
|
|
318
|
+
ensureDir(path.join(projectPath, 'tests', 'step-definitions'));
|
|
319
|
+
ensureDir(path.join(projectPath, 'tests', 'selectors'));
|
|
320
|
+
ensureDir(path.join(projectPath, 'screenshots'));
|
|
321
|
+
|
|
322
|
+
const assetResults = writeTextAssets(projectPath, opts);
|
|
323
|
+
const tsconfigWritten = writeTsconfig(projectPath, opts);
|
|
324
|
+
const pkgResult = ensurePackageJson(projectPath, opts);
|
|
325
|
+
|
|
326
|
+
for (const r of assetResults) {
|
|
327
|
+
console.log(` [${r.written ? 'wrote ' : 'kept '}] ${r.file}`);
|
|
328
|
+
}
|
|
329
|
+
console.log(` [${tsconfigWritten ? 'wrote ' : 'kept '}] tsconfig.json`);
|
|
330
|
+
console.log(
|
|
331
|
+
` [${pkgResult.created ? 'wrote ' : 'merged'}] package.json`
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
if (!opts.skipBrowsers) installBrowsers(projectPath);
|
|
335
|
+
|
|
336
|
+
console.log('\n [webship-js] Setup complete!\n');
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (require.main === module) {
|
|
340
|
+
run(process.argv.slice(2));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
module.exports = { run };
|
package/bin/postinstall.js
CHANGED
|
@@ -4,15 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
const fs = require('fs');
|
|
6
6
|
const path = require('path');
|
|
7
|
-
const {
|
|
7
|
+
const { run } = require('./init-webship');
|
|
8
8
|
|
|
9
|
-
// The directory where `npm install` was invoked (the user's project root)
|
|
10
|
-
const projectPath = process.env.INIT_CWD || process.cwd();
|
|
11
|
-
|
|
12
|
-
// The webship-js package directory
|
|
13
9
|
const packagePath = path.join(__dirname, '..');
|
|
10
|
+
const projectPath = process.env.INIT_CWD || process.cwd();
|
|
14
11
|
|
|
15
|
-
// Skip
|
|
12
|
+
// Skip when invoked from webship-js itself (development / CI).
|
|
16
13
|
if (
|
|
17
14
|
projectPath === packagePath ||
|
|
18
15
|
projectPath.startsWith(path.join(packagePath, 'node_modules'))
|
|
@@ -20,73 +17,10 @@ if (
|
|
|
20
17
|
process.exit(0);
|
|
21
18
|
}
|
|
22
19
|
|
|
23
|
-
//
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
fs.mkdirSync(dest, { recursive: true });
|
|
28
|
-
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
29
|
-
const srcPath = path.join(src, entry.name);
|
|
30
|
-
const destPath = path.join(dest, entry.name);
|
|
31
|
-
if (entry.isDirectory()) {
|
|
32
|
-
copyDirSync(srcPath, destPath);
|
|
33
|
-
} else {
|
|
34
|
-
fs.copyFileSync(srcPath, destPath);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// Only copy template files on a fresh install (tests/ not yet present)
|
|
40
|
-
const testsTarget = path.join(projectPath, 'tests');
|
|
41
|
-
if (!fs.existsSync(testsTarget)) {
|
|
42
|
-
console.log('\n [webship-js] Installing required files...\n');
|
|
43
|
-
|
|
44
|
-
copyDirSync(path.join(requiredDir, 'tests'), testsTarget);
|
|
45
|
-
|
|
46
|
-
fs.copyFileSync(
|
|
47
|
-
path.join(requiredDir, 'generate-reports.js'),
|
|
48
|
-
path.join(projectPath, 'generate-reports.js')
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
fs.copyFileSync(
|
|
52
|
-
path.join(requiredDir, 'cucumber.js'),
|
|
53
|
-
path.join(projectPath, 'cucumber.js')
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
fs.copyFileSync(
|
|
57
|
-
path.join(requiredDir, 'tsconfig.json'),
|
|
58
|
-
path.join(projectPath, 'tsconfig.json')
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
fs.copyFileSync(
|
|
62
|
-
path.join(requiredDir, 'playwright.config.ts'),
|
|
63
|
-
path.join(projectPath, 'playwright.config.ts')
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
// Only copy package.json if the project does not have one yet
|
|
67
|
-
const targetPkgJson = path.join(projectPath, 'package.json');
|
|
68
|
-
if (!fs.existsSync(targetPkgJson)) {
|
|
69
|
-
fs.copyFileSync(path.join(requiredDir, 'package.json'), targetPkgJson);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Always ensure the test scripts are present in package.json
|
|
74
|
-
const targetPkgJson = path.join(projectPath, 'package.json');
|
|
75
|
-
if (fs.existsSync(targetPkgJson)) {
|
|
76
|
-
const pkgJson = JSON.parse(fs.readFileSync(targetPkgJson, 'utf8'));
|
|
77
|
-
pkgJson.scripts = pkgJson.scripts || {};
|
|
78
|
-
pkgJson.scripts.test = 'cucumber-js --config cucumber.js; node generate-reports.js;';
|
|
79
|
-
pkgJson.scripts['test:firefox'] = 'BROWSER=firefox cucumber-js --config cucumber.js; node generate-reports.js;';
|
|
80
|
-
pkgJson.scripts['test:webkit'] = 'BROWSER=webkit cucumber-js --config cucumber.js; node generate-reports.js;';
|
|
81
|
-
fs.writeFileSync(targetPkgJson, JSON.stringify(pkgJson, null, 2) + '\n', 'utf8');
|
|
20
|
+
// Only run on a fresh project — cucumber.js absence marks an uninitialized project.
|
|
21
|
+
// Re-installs (npm install, npm ci) will not overwrite user customizations.
|
|
22
|
+
if (fs.existsSync(path.join(projectPath, 'cucumber.js'))) {
|
|
23
|
+
process.exit(0);
|
|
82
24
|
}
|
|
83
25
|
|
|
84
|
-
|
|
85
|
-
console.log('\n [webship-js] Installing Playwright browsers...\n');
|
|
86
|
-
spawnSync('npx', ['playwright', 'install', '--with-deps', 'chromium'], {
|
|
87
|
-
cwd: projectPath,
|
|
88
|
-
stdio: 'inherit',
|
|
89
|
-
shell: true,
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
console.log('\n [webship-js] Setup complete!\n');
|
|
26
|
+
run(['--cwd', projectPath]);
|