forked from adobecom/nala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
146 lines (121 loc) · 4.26 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
const { spawn } = require('child_process');
function displayHelp() {
console.log(`
Usage: node run.js [options]
Options:
-p, --project Specify the project to run tests on, e.g., milo-live-chrome (required).
Use 'all' for all projects, or comma-separated list of projects.
-c, --config Specify name of configuration file, e.g., milo (required).
-g, --grep Filter tests by grep pattern, e.g., '@milo'.
-r, --reporter Specify the reporter to use, e.g., 'html'.
-h, --help Display this help message and exit.
--headed Run tests in headed mode.
--headless Run tests in headless mode (default).
--list List all tests found, but doesn't run them.
Examples:
node run.js -p=milo-live-chrome -c=milo
node run.js --project milo-live-chrome --config milo --grep '@milo' --headed
node run.js -h
`);
}
function parseArgs(args) {
const result = {};
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
// Check for help option
if (arg === '-h' || arg === '--help') {
displayHelp();
process.exit(0); // Exit after displaying help
}
// Handle both short and long options
if (arg.startsWith('-')) {
const isLongOption = arg.startsWith('--');
const hasEqualSign = arg.indexOf('=') !== -1;
let key;
let value;
if (hasEqualSign) {
// Split on the first '=' found
const splitIndex = arg.indexOf('=');
key = arg.slice(isLongOption ? 2 : 1, splitIndex);
value = arg.slice(splitIndex + 1);
// Normalize key for known long options
if (key === 'project' || key === 'p') key = 'project';
if (key === 'config' || key === 'c') key = 'config';
if (key === 'grep' || key === 'g') key = 'grep';
if (key === 'reporter' || key === 'r') key = 'reporter';
result[key] = value;
} else {
// If just -option, set it to true (boolean flag)
key = arg.slice(isLongOption ? 2 : 1);
// Normalize key for known long options
if (key === 'project' || key === 'p') key = 'project';
if (key === 'config' || key === 'c') key = 'config';
if (key === 'grep' || key === 'g') key = 'grep';
if (key === 'reporter' || key === 'r') key = 'reporter';
// Check next argument to see if it's a standalone value or another flag
if (args[i + 1] && !args[i + 1].startsWith('-')) {
result[key] = args[i + 1];
i += 1; // Skip next arg since it's consumed as the value of this option
} else {
result[key] = true;
}
}
}
}
return result;
}
function runPlaywrightTests() {
// Start from the first actual argument (skip node and script paths)
const args = process.argv.slice(2);
const argv = parseArgs(args);
// Construct the Playwright CLI command
const command = 'npx playwright test';
const options = [];
if (Object.keys(argv).length === 0) {
console.log('No options provided, displaying help.');
displayHelp();
process.exit(0);
}
// Add options dynamically based on argv
if (argv.headed) {
options.push('--headed');
}
if (argv.list) {
options.push('--list');
}
if (argv.project) {
if (argv.project === 'all') {
console.log('Running tests for all projects');
} else {
const projectArray = argv.project.split(',');
projectArray.forEach((project) => {
options.push(`--project=${project}`);
});
}
} else {
console.error('Error: -p (project) is required');
process.exit(1);
}
if (argv.config) {
options.push(`--config=configs/${argv.config}.config.js`);
} else {
console.error('Error: -c (config) is required');
process.exit(1);
}
if (argv.grep) {
options.push(`--grep="${argv.grep}"`);
}
if (argv.reporter) {
options.push(`--reporter=${argv.reporter}`);
}
// More flags can be dynamically handled here
const finalCommand = `${command} ${options.join(' ')}`;
console.log(`Executing command: ${finalCommand}`);
// Spawn the Playwright test process
const testProcess = spawn(finalCommand, { stdio: 'inherit', shell: true });
testProcess.on('close', (code) => {
console.log(`Playwright tests exited with code ${code}`);
process.exit(code);
});
}
runPlaywrightTests();