Skip to content

Commit 161f710

Browse files
authored
Update restart.test.js
1 parent 41e6a52 commit 161f710

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

tests/e2e/restart.test.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,32 @@ const { test, expect } = require('@playwright/test');
33
const { execSync } = require('child_process');
44
const os = require('os');
55

6-
function isElectronRunning() {
6+
function isElectronRunning(pid) {
77
try {
88
const platform = os.platform();
9+
let output;
910

10-
// Check if Electron process is running based on the operating system
1111
if (platform === 'win32') {
12-
const output = execSync('tasklist').toString();
12+
output = execSync(`tasklist /FI "PID eq ${pid}"`).toString();
1313
return output.includes('electron.exe');
14-
} else if (platform === 'darwin') {
15-
const output = execSync('ps -A').toString();
14+
} else if (platform === 'darwin' || platform === 'linux') {
15+
output = execSync(`ps -p ${pid}`).toString();
1616
return output.includes('Electron');
17-
} else if (platform === 'linux') {
18-
const output = execSync('pgrep electron').toString();
19-
return output.trim() !== '';
2017
}
2118
} catch (error) {
2219
console.error('Error checking for Electron process:', error);
2320
return false;
2421
}
2522
}
2623

27-
function forceKillElectron() {
24+
function forceKillElectron(pid) {
2825
try {
2926
const platform = os.platform();
3027

3128
if (platform === 'win32') {
32-
execSync('taskkill /F /IM electron.exe');
29+
execSync(`taskkill /PID ${pid} /F`);
3330
} else if (platform === 'darwin' || platform === 'linux') {
34-
execSync('pkill -f Electron');
31+
process.kill(pid, 'SIGKILL');
3532
}
3633
console.log('Electron process forcefully terminated.');
3734
} catch (error) {
@@ -45,22 +42,23 @@ test('Launch and close Electron app 10 times', async () => {
4542

4643
// Launch the Electron app
4744
const app = await electron.launch({ args: ['main.js'] });
45+
const pid = app.process().pid;
4846
const window = await app.firstWindow();
4947

5048
// Close the app
5149
await app.close();
5250

53-
// Wait for a moment to allow for process termination
54-
await new Promise((resolve) => setTimeout(resolve, 2000));
51+
// Waiting for a moment to allow for process termination
52+
await new Promise((resolve) => setTimeout(resolve, 3000));
5553

5654
// Check if the Electron app is still running
57-
let running = isElectronRunning();
55+
let running = isElectronRunning(pid);
5856
if (running) {
5957
console.warn(`Iteration ${i + 1}: Electron app is still running. Attempting to force kill.`);
60-
forceKillElectron();
58+
forceKillElectron(pid);
6159

6260
// Re-check if the process is still running after the forced kill
63-
running = isElectronRunning();
61+
running = isElectronRunning(pid);
6462
}
6563

6664
// Assert that the app is not running

0 commit comments

Comments
 (0)