Skip to content

Commit

Permalink
chore(e2e): timeout calls to browser.saveScreenshot() and catch the e…
Browse files Browse the repository at this point in the history
…rror (mongodb-js#5725)

* timeout saveScreenshot and catch the error

* reject with an error object
  • Loading branch information
lerouxb authored Apr 23, 2024
1 parent 35590b4 commit f2ab258
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion packages/compass-e2e-tests/helpers/commands/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,37 @@ import path from 'path';
import type { CompassBrowser } from '../compass-browser';
import { SCREENSHOTS_PATH } from '../compass';

const withTimeout = (millis: number, promise: Promise<any>) => {
let timeoutPid: NodeJS.Timeout;
const timeout = new Promise(
(resolve, reject) =>
(timeoutPid = setTimeout(
() => reject(new Error(`Timed out after ${millis} ms.`)),
millis
))
);
return Promise.race([promise, timeout]).finally(() => {
if (timeoutPid) {
clearTimeout(timeoutPid);
}
});
};

export async function screenshot(
browser: CompassBrowser,
filename: string
): Promise<void> {
// Give animations a second. Hard to have a generic way to know if animations
// are still in progress or not.
await browser.pause(1000);
await browser.saveScreenshot(path.join(SCREENSHOTS_PATH, filename));

const fullPath = path.join(SCREENSHOTS_PATH, filename);
try {
await withTimeout(10000, browser.saveScreenshot(fullPath));
} catch (err: any) {
// For some reason browser.saveScreenshot() sometimes times out on mac with
// `WARN webdriver: Request timed out! Consider increasing the
// "connectionRetryTimeout" option.`. The default is 120 seconds.
console.error(`Unable to save screenshot: ${fullPath}`);
}
}

0 comments on commit f2ab258

Please sign in to comment.