| 30 | } |
| 31 | |
| 32 | public async launch(launchOptions: BrowserLauncherOptions): Promise<void> { |
| 33 | this.launchOptions = launchOptions; |
| 34 | |
| 35 | if (this.seleniumProcess) { |
| 36 | await this.close(); |
| 37 | } |
| 38 | |
| 39 | const projectRoot = path.resolve(dirname(fileURLToPath(import.meta.url)), "../../"); |
| 40 | const seleniumServerPath = path.join(projectRoot, "selenium", "server", "selenium-server.jar"); |
| 41 | |
| 42 | const seleniumArgs = ["-jar", seleniumServerPath, "standalone"]; |
| 43 | |
| 44 | this.seleniumProcess = spawn("java", seleniumArgs); |
| 45 | this.seleniumServerUrl = `http://localhost:${this.port}`; |
| 46 | |
| 47 | this.seleniumProcess.stdout?.on("data", (data) => { |
| 48 | this.logger.info(`Selenium stdout: ${data}`); |
| 49 | this.postLog({ |
| 50 | type: BrowserEventType.Console, |
| 51 | text: JSON.stringify({ type: BrowserEventType.Console, message: `${data}` }), |
| 52 | timestamp: new Date(), |
| 53 | }); |
| 54 | }); |
| 55 | |
| 56 | this.seleniumProcess.stderr?.on("data", (data) => { |
| 57 | this.logger.error(`Selenium stderr: ${data}`); |
| 58 | this.postLog({ |
| 59 | type: BrowserEventType.Error, |
| 60 | text: JSON.stringify({ type: BrowserEventType.Error, error: `${data}` }), |
| 61 | timestamp: new Date(), |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | this.seleniumProcess.on("close", (code) => { |
| 66 | this.logger.info(`Selenium process exited with code ${code}`); |
| 67 | this.seleniumProcess = null; |
| 68 | }); |
| 69 | |
| 70 | await new Promise<void>((resolve, reject) => { |
| 71 | const timeout = setTimeout(() => { |
| 72 | reject(new Error("Selenium server failed to start within the timeout period")); |
| 73 | }, 15000); // 15 seconds timeout |
| 74 | |
| 75 | this.seleniumProcess!.stdout?.on("data", (data) => { |
| 76 | if (data.toString().includes("Started Selenium Standalone")) { |
| 77 | clearTimeout(timeout); |
| 78 | resolve(); |
| 79 | } |
| 80 | }); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | public close(): void { |
| 85 | if (this.seleniumProcess) { |