-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcucumber.conf.js
More file actions
202 lines (178 loc) · 5.85 KB
/
cucumber.conf.js
File metadata and controls
202 lines (178 loc) · 5.85 KB
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
const Nightwatch = require("nightwatch");
const {
After,
AfterAll,
Before,
setDefaultTimeout,
BeforeAll,
} = require("@cucumber/cucumber");
const fs = require("fs");
const fsPromises = fs.promises;
const path = require("path");
const os = require("os");
require("events").EventEmitter.defaultMaxListeners = 20;
setDefaultTimeout(300000); // Increase timeout to 5 minutes
BeforeAll(async function () {
try {
fs.mkdirSync("report", { recursive: true });
fs.mkdirSync("screenshots", { recursive: true });
} catch (err) {
console.error("Error creating directories:", err.message);
}
});
Before(async function ({ pickle }) {
this.tmpUserDataDir = fs.mkdtempSync(
path.join(os.tmpdir(), "nw-chrome-profile-")
);
console.log("tmpUserDataDir:", this.tmpUserDataDir);
const chromeArgs = [
"--no-sandbox",
"--disable-dev-shm-usage",
"--disable-extensions",
"--disable-gpu",
"--disable-background-networking",
"--disable-sync",
"--metrics-recording-only",
"--disable-default-apps",
"--mute-audio",
"--no-first-run",
"--ignore-certificate-errors",
"--allow-insecure-localhost",
"--window-size=1920,1080",
];
const isDebug = pickle.tags?.some(tag => tag.name === '@debug');
if (!isDebug) chromeArgs.push("--headless=new");
const webdriver = {};
if (this.parameters["webdriver-host"]) webdriver.host = this.parameters["webdriver-host"];
if (this.parameters["webdriver-port"]) webdriver.port = this.parameters["webdriver-port"];
if (typeof this.parameters["start-process"] !== "undefined")
webdriver.start_process = this.parameters["start-process"];
const globals = {};
if (this.parameters["retry-interval"])
globals.waitForConditionPollInterval = this.parameters["retry-interval"];
this.client = Nightwatch.createClient({
headless: this.parameters.headless,
env: this.parameters.env,
timeout: this.parameters.timeout,
parallel: !!this.parameters.parallel,
output: !this.parameters["disable-output"],
enable_global_apis: true,
silent: !this.parameters.verbose,
always_async_commands: true,
webdriver,
persist_globals: this.parameters["persist-globals"],
config: this.parameters.config,
globals: {
run: {},
},
desiredCapabilities: {
browserName: "chrome",
"goog:chromeOptions": {
args: chromeArgs,
},
},
});
if (this.client.settings.sync_test_names) {
this.client.updateCapabilities({ name: pickle.name });
}
console.log("Launching Chrome with args:", chromeArgs);
console.log("Executing test:", pickle.name);
try {
this.browser = await this.client.launchBrowser();
this.browser.globals.timestamp = Date.now();
} catch (err) {
console.error("Failed to launch browser:", err.message);
if (this.attach) {
this.attach(`Browser launch failed: ${err.message}`);
}
this.skipScenario = true;
}
});
After(async function (testCase) {
try {
if (testCase.result.status === "FAILED" && this.browser) {
try {
// Take screenshot if test failed and browser is available
const filename = `screenshots/${testCase.pickle.name}-${Date.now()}.png`;
await this.browser.saveScreenshot(filename);
this.attach(fs.readFileSync(filename), "image/png");
} catch (screenshotError) {
console.error("Failed to save screenshot:", screenshotError.message);
}
}
const isDebug = testCase.pickle?.tags?.some(tag => tag.name === '@debug');
if (this.browser && !isDebug) {
try {
// Only quit browser if not running with @debug tag
await this.browser.quit();
} catch (quitError) {
console.error("Failed to quit browser:", quitError.message);
}
}
if (this.tmpUserDataDir) {
try {
// Clean up temp directory
fs.rmSync(this.tmpUserDataDir, { recursive: true, force: true });
} catch (cleanupError) {
console.error("Failed to clean up temp directory:", cleanupError.message);
}
}
} catch (error) {
console.error("Error in After hook:", error.message);
}
const runJsonPath = "report/run.json";
if (
this.browser?.capabilities &&
!this.browser?.globals?.run?.browserName
) {
const caps = this.browser.capabilities;
const globalsRun = this.browser.globals.run;
globalsRun.browserName = caps.browserName;
globalsRun.version = caps.browserVersion;
globalsRun.platform = caps.platformName;
try {
if (!fs.existsSync(runJsonPath)) {
await fsPromises.writeFile(runJsonPath, JSON.stringify(globalsRun, null, 2));
console.log("Saved run metadata.");
}
} catch (err) {
console.error("Error saving run metadata:", err.message);
}
}
});
AfterAll(async function () {
// Kill ChromeDriver process
const { spawnSync } = require('child_process');
const isWin = process.platform === 'win32';
if (isWin) {
spawnSync('taskkill', ['/IM', 'chromedriver.exe', '/F']);
} else {
spawnSync('pkill', ['-f', 'chromedriver']);
}
// Delay before final exit to allow async flush
setTimeout(() => {
console.log("⚠️ Forcing process exit after delay.");
process.exit(0);
}, 1000);
});
// Handle interrupts and exit signals
process.on('SIGINT', async () => {
console.log('\nReceived interrupt signal - Running cleanup...');
await new Promise(resolve => setTimeout(resolve, 3000));
// Allow AfterAll to run naturally
});
setTimeout(() => {
console.log('⚠️ Node is still alive after 10 seconds.');
}, 10000);
process.on('beforeExit', (code) => {
console.log(`[DEBUG] beforeExit triggered. Code: ${code}`);
});
process.on('exit', (code) => {
console.log(`[DEBUG] Process exited. Code: ${code}`);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('❗ Unhandled Rejection:', reason);
});
process.on('SIGTERM', () => {
console.log('❗ SIGTERM received');
});