@@ -3,35 +3,32 @@ const { test, expect } = require('@playwright/test');
3
3
const { execSync } = require ( 'child_process' ) ;
4
4
const os = require ( 'os' ) ;
5
5
6
- function isElectronRunning ( ) {
6
+ function isElectronRunning ( pid ) {
7
7
try {
8
8
const platform = os . platform ( ) ;
9
+ let output ;
9
10
10
- // Check if Electron process is running based on the operating system
11
11
if ( platform === 'win32' ) {
12
- const output = execSync ( ' tasklist' ) . toString ( ) ;
12
+ output = execSync ( ` tasklist /FI "PID eq ${ pid } "` ) . toString ( ) ;
13
13
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 ( ) ;
16
16
return output . includes ( 'Electron' ) ;
17
- } else if ( platform === 'linux' ) {
18
- const output = execSync ( 'pgrep electron' ) . toString ( ) ;
19
- return output . trim ( ) !== '' ;
20
17
}
21
18
} catch ( error ) {
22
19
console . error ( 'Error checking for Electron process:' , error ) ;
23
20
return false ;
24
21
}
25
22
}
26
23
27
- function forceKillElectron ( ) {
24
+ function forceKillElectron ( pid ) {
28
25
try {
29
26
const platform = os . platform ( ) ;
30
27
31
28
if ( platform === 'win32' ) {
32
- execSync ( ' taskkill /F /IM electron.exe' ) ;
29
+ execSync ( ` taskkill /PID ${ pid } /F` ) ;
33
30
} else if ( platform === 'darwin' || platform === 'linux' ) {
34
- execSync ( 'pkill -f Electron ') ;
31
+ process . kill ( pid , 'SIGKILL ') ;
35
32
}
36
33
console . log ( 'Electron process forcefully terminated.' ) ;
37
34
} catch ( error ) {
@@ -45,22 +42,23 @@ test('Launch and close Electron app 10 times', async () => {
45
42
46
43
// Launch the Electron app
47
44
const app = await electron . launch ( { args : [ 'main.js' ] } ) ;
45
+ const pid = app . process ( ) . pid ;
48
46
const window = await app . firstWindow ( ) ;
49
47
50
48
// Close the app
51
49
await app . close ( ) ;
52
50
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 ) ) ;
55
53
56
54
// Check if the Electron app is still running
57
- let running = isElectronRunning ( ) ;
55
+ let running = isElectronRunning ( pid ) ;
58
56
if ( running ) {
59
57
console . warn ( `Iteration ${ i + 1 } : Electron app is still running. Attempting to force kill.` ) ;
60
- forceKillElectron ( ) ;
58
+ forceKillElectron ( pid ) ;
61
59
62
60
// Re-check if the process is still running after the forced kill
63
- running = isElectronRunning ( ) ;
61
+ running = isElectronRunning ( pid ) ;
64
62
}
65
63
66
64
// Assert that the app is not running
0 commit comments