Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add global default timeout #41

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,5 @@ dist

# TernJS port file
.tern-port

__assets__
11 changes: 10 additions & 1 deletion src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ import { replay } from './runner.mjs';
// noop
},
async (argv) => {
await replay(argv.runCfgPath as string, argv.suiteName as string);
try {
const passed = await replay(
argv.runCfgPath as string,
argv.suiteName as string
);
process.exit(passed ? 0 : 1);
} catch (err) {
console.error(err);
process.exit(1);
}
}
)
.option('runCfgPath', {
Expand Down
42 changes: 32 additions & 10 deletions src/runner.mts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type LogEntry = {
type Suite = {
name: string;
recording: string;
timeout?: number;
};

type RunConfig = {
Expand Down Expand Up @@ -135,20 +136,41 @@ export async function replay(runCfgPath: string, suiteName: string) {
throw new Error(`Could not find suite named '${suiteName}'`);
}

// saucectl suite.timeout is in nanoseconds, convert to seconds
const timeout = (suite.timeout || 0) / 1_000_000_000 || 30 * 60; // 30min default

const timeoutPromise = new Promise<boolean>((resolve) => {
setTimeout(() => {
console.error(`Job timed out after ${timeout} seconds`);
resolve(false);
}, timeout * 1000);
});

// Validate & parse the file.
const recording = parseRecording(suite.recording);

const browser = await puppeteer.launch({
headless: false,
product: process.env.BROWSER_NAME as Product,
executablePath: process.env.BROWSER_PATH,
});
return Promise.race([timeoutPromise, runReplay(recording)]);
}

const page = await browser.newPage();
async function runReplay(recording: UserFlow) {
try {
const browser = await puppeteer.launch({
headless: false,
product: process.env.BROWSER_NAME as Product,
executablePath: process.env.BROWSER_PATH,
});

const page = await browser.newPage();

// Create a runner and execute the script.
const runner = await createRunner(recording, new Extension(browser, page));
// Create a runner and execute the script.
const runner = await createRunner(recording, new Extension(browser, page));

await runner.run();
await browser.close();
await runner.run();
await browser.close();

return true;
} catch (e) {
console.error('Error running replay:', e.message);
return false;
}
}