|
| 1 | +const { _electron: electron } = require('playwright'); |
| 2 | +const { test, expect } = require('@playwright/test'); |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const os = require('os'); |
| 5 | + |
| 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) { |
| 38 | + try { |
| 39 | + const platform = os.platform(); |
| 40 | + let output; |
| 41 | + |
| 42 | + if (platform === 'win32') { |
| 43 | + output = execSync(`tasklist /FI "PID eq ${pid}"`).toString(); |
| 44 | + return output.includes(`${pid}`); |
| 45 | + } else if (platform === 'darwin' || platform === 'linux') { |
| 46 | + output = execSync(`ps -aux | grep ${pid}`).toString(); |
| 47 | + return output.includes(`main.js`); |
| 48 | + } |
| 49 | + } catch (error) { |
| 50 | + console.error(`Error checking for process ${pid}:`, error.message); |
| 51 | + return false; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +test('Launch and close Electron app 10 times, ensuring backend termination', async () => { |
| 56 | + for (let i = 0; i < 10; i++) { |
| 57 | + console.log(`Iteration ${i + 1}: Launching and closing Electron app.`); |
| 58 | + |
| 59 | + // Launch the Electron app |
| 60 | + const app = await electron.launch({ args: ['main.js'] }); |
| 61 | + const pid = app.process().pid; |
| 62 | + 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'); |
| 67 | + |
| 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 |
| 82 | + await app.close(); |
| 83 | + // Wait for a moment to allow processes to terminate |
| 84 | + await new Promise((resolve) => setTimeout(resolve, 3000)); |
| 85 | + // Check if the Electron app is still running |
| 86 | + let frontendRunning = isProcessRunning(pid); |
| 87 | + if (frontendRunning) { |
| 88 | + console.error(`Iteration ${i + 1}: Electron app could not be terminated.`); |
| 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.`); |
| 100 | + } |
| 101 | + } |
| 102 | +}); |
0 commit comments