Skip to content

Commit 25363ce

Browse files
authored
Await driver.quit (#209)
- Use await "driver.quit()" to properly await the browser and driver shutdown. - Print safaridriver logs in case of failures (to be removed once we're stable again)
1 parent eda9143 commit 25363ce

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

tests/run-browser.mjs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
// THE POSSIBILITY OF SUCH DAMAGE.
2626

2727
import serve from "./server.mjs";
28-
import { Builder, Capabilities } from "selenium-webdriver";
28+
import { Builder, Capabilities, logging } from "selenium-webdriver";
2929
import commandLineArgs from "command-line-args";
30+
import { promises as fs } from "fs";
31+
import path from "path";
32+
import os from "os";
3033

3134
import {logInfo, logError, printHelp, runTest} from "./helper.mjs";
3235

@@ -36,7 +39,6 @@ const optionDefinitions = [
3639
{ name: "help", alias: "h", description: "Print this help text." },
3740
];
3841

39-
4042
const options = commandLineArgs(optionDefinitions);
4143

4244
if ("help" in options)
@@ -50,6 +52,7 @@ let capabilities;
5052
switch (BROWSER) {
5153
case "safari":
5254
capabilities = Capabilities.safari();
55+
capabilities.set("safari:diagnose", true);
5356
break;
5457

5558
case "firefox": {
@@ -102,13 +105,15 @@ async function runEnd2EndTest(name, params) {
102105

103106
async function testEnd2End(params) {
104107
const driver = await new Builder().withCapabilities(capabilities).build();
108+
const sessionId = (await driver.getSession()).getId();
105109
const driverCapabilities = await driver.getCapabilities();
106110
logInfo(`Browser: ${driverCapabilities.getBrowserName()} ${driverCapabilities.getBrowserVersion()}`);
107111
const urlParams = Object.assign({
108112
worstCaseCount: 2,
109113
iterationCount: 3
110114
}, params);
111115
let results;
116+
let success = true;
112117
try {
113118
const url = new URL(`http://localhost:${PORT}/index.html`);
114119
url.search = new URLSearchParams(urlParams).toString();
@@ -126,9 +131,13 @@ async function testEnd2End(params) {
126131
results = await benchmarkResults(driver);
127132
// FIXME: validate results;
128133
} catch(e) {
134+
success = false;
129135
throw e;
130136
} finally {
131-
driver.quit();
137+
await driver.quit();
138+
if (!success) {
139+
await printLogs(sessionId);
140+
}
132141
}
133142
}
134143

@@ -152,7 +161,6 @@ class JetStreamTestError extends Error {
152161
super(`Tests failed: ${errors.map(e => e.stack).join(", ")}`);
153162
this.errors = errors;
154163
}
155-
156164
}
157165

158166
const UPDATE_INTERVAL = 250;
@@ -187,4 +195,28 @@ function logIncrementalResult(previousResults, benchmarkResults) {
187195
}
188196
}
189197

198+
function printLogs(sessionId) {
199+
if (BROWSER === "safari" && sessionId)
200+
return printSafariLogs(sessionId);
201+
}
202+
203+
async function printSafariLogs(sessionId) {
204+
const sessionLogDir = path.join(os.homedir(), "Library", "Logs", "com.apple.WebDriver", sessionId);
205+
try {
206+
const files = await fs.readdir(sessionLogDir);
207+
const logFiles = files.filter(f => f.startsWith("safaridriver.") && f.endsWith(".txt"));
208+
if (logFiles.length === 0) {
209+
logInfo(`No safaridriver log files found in session directory: ${sessionLogDir}`);
210+
return;
211+
}
212+
for (const file of logFiles) {
213+
const logPath = path.join(sessionLogDir, file);
214+
const logContent = await fs.readFile(logPath, "utf8");
215+
logGroup(`SafariDriver Log: ${file}`, () => console.log(logContent));
216+
}
217+
} catch (err) {
218+
logError("Error reading SafariDriver logs:", err);
219+
}
220+
}
221+
190222
setImmediate(runTests);

0 commit comments

Comments
 (0)