@@ -3,49 +3,100 @@ const { test, expect } = require('@playwright/test');
33const { execSync } = require ( 'child_process' ) ;
44const os = require ( 'os' ) ;
55
6- function isElectronRunning ( pid ) {
6+ // Helper function to find a process ID by name
7+ function getProcessIdByName ( processName , pid ) {
8+ try {
9+ const platform = os . platform ( ) ;
10+ let output ;
11+
12+ if ( platform === 'win32' ) {
13+ // Fetch all processes
14+ output = execSync ( `tasklist /FO CSV` ) . toString ( ) ;
15+ const lines = output . split ( '\n' ) ;
16+ for ( const line of lines ) {
17+ if ( line . includes ( processName ) ) {
18+ const parts = line . split ( ',' ) ;
19+ const pid = parseInt ( parts [ 1 ] . replace ( / " / g, '' ) . trim ( ) , 10 ) ;
20+ console . log ( `Found process: ${ line } ` ) ;
21+ return pid ; // Return the PID of the process
22+ }
23+ }
24+ } else if ( platform === 'darwin' || platform === 'linux' ) {
25+ // Fetch the PID for Unix-based systems
26+ output = execSync ( `pgrep "${ processName } " -P ${ pid } ` ) . toString ( ) ;
27+ console . log ( `Found backend PID: ${ output . trim ( ) } ` ) ;
28+ return parseInt ( output . trim ( ) , 10 ) ; // Return the PID
29+ }
30+ } catch ( error ) {
31+ console . error ( `Error fetching process ID for ${ processName } :` , error . message ) ;
32+ return null ;
33+ }
34+ }
35+
36+ // Helper function to check if a process is running by PID
37+ function isProcessRunning ( pid ) {
738 try {
839 const platform = os . platform ( ) ;
940 let output ;
1041
1142 if ( platform === 'win32' ) {
1243 output = execSync ( `tasklist /FI "PID eq ${ pid } "` ) . toString ( ) ;
13- return output . includes ( 'electron.exe' ) ;
44+ return output . includes ( ` ${ pid } ` ) ;
1445 } else if ( platform === 'darwin' || platform === 'linux' ) {
15- output = execSync ( `ps -p ${ pid } ` ) . toString ( ) ;
16- return output . includes ( 'Electron' ) ;
46+ output = execSync ( `ps -aux | grep ${ pid } ` ) . toString ( ) ;
47+ return output . includes ( `main.js` ) ;
1748 }
1849 } catch ( error ) {
19- console . error ( ' Error checking for Electron process:' , error ) ;
50+ console . error ( ` Error checking for process ${ pid } :` , error . message ) ;
2051 return false ;
2152 }
2253}
2354
24- test ( 'Launch and close Electron app 10 times' , async ( ) => {
55+ test ( 'Launch and close Electron app 10 times, ensuring backend termination ' , async ( ) => {
2556 for ( let i = 0 ; i < 10 ; i ++ ) {
2657 console . log ( `Iteration ${ i + 1 } : Launching and closing Electron app.` ) ;
2758
2859 // Launch the Electron app
2960 const app = await electron . launch ( { args : [ 'main.js' ] } ) ;
3061 const pid = app . process ( ) . pid ;
3162 const window = await app . firstWindow ( ) ;
63+ console . log ( `Frontend PID: ${ pid } ` )
64+ // Selecting the device (MPW1 Gemini)
65+ const deviceDropdown = await window . waitForSelector ( '#deviceId' ) ;
66+ await deviceDropdown . selectOption ( 'MPW1' ) ;
3267
33- // Selecting the device (MPW1 Gemini)
34- const deviceDropdown = await window . waitForSelector ( '#deviceId' ) ;
35- await deviceDropdown . selectOption ( 'MPW1' ) ;
36-
37-
38- // Close the app
68+ let backendProcessName = '' ;
69+ if ( os . platform ( ) === 'win32' ) {
70+ backendProcessName = 'python.exe' ;
71+ } else if ( os . platform ( ) === 'darwin' || os . platform ( ) === 'linux' ) {
72+ backendProcessName = 'python' ;
73+ }
74+ console . log ( `The backend process name is: ${ backendProcessName } ` ) ;
75+ const backendPid = getProcessIdByName ( backendProcessName , pid ) ;
76+ if ( ! backendPid ) {
77+ console . error ( 'Failed to fetch backend PID.' ) ;
78+ break ;
79+ }
80+ console . log ( `Backend PID: ${ backendPid } ` ) ;
81+ // Close the Electron app
3982 await app . close ( ) ;
40-
41- // Waiting for a moment to allow for process termination
42- await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) ) ;
43-
83+ // Wait for a moment to allow processes to terminate
84+ await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) ) ;
4485 // Check if the Electron app is still running
45- let running = isElectronRunning ( pid ) ;
46- if ( running ) {
86+ let frontendRunning = isProcessRunning ( pid ) ;
87+ if ( frontendRunning ) {
4788 console . error ( `Iteration ${ i + 1 } : Electron app could not be terminated.` ) ;
48- break ; // Stop further iterations if the app cannot be killed
89+ break ;
90+ }
91+ // Check if the backend process is still running
92+ let backendRunning = isProcessRunning ( backendPid ) ;
93+ if ( backendRunning ) {
94+ console . error (
95+ `Iteration ${ i + 1 } : Backend process ${ backendPid } could not be terminated.`
96+ ) ;
97+ break ;
98+ } else {
99+ console . log ( `Iteration ${ i + 1 } : Backend process terminated successfully.` ) ;
49100 }
50101 }
51- } ) ;
102+ } ) ;
0 commit comments