Skip to content

Commit

Permalink
Close playwright browser if an error occurs (#71)
Browse files Browse the repository at this point in the history
* close playwright browser if an error occurs
  • Loading branch information
Miłosz Skaza authored Feb 22, 2023
1 parent be1673e commit f305b6d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/utils/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export class PlaywrightRunner {
try {
await (async () => vm.run(preOpenAction))();
} catch (e) {
await this.teardown();
throw new Error(`invalid action "${preOpenAction}"`);
}
}
Expand All @@ -117,6 +118,7 @@ export class PlaywrightRunner {
await (async () => vm.run(postOpenAction))();
await this.page.waitForLoadState();
} catch (e) {
await this.teardown();
throw new Error(`invalid action "${postOpenAction}"`);
}
}
Expand Down
75 changes: 74 additions & 1 deletion tests/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,4 +646,77 @@ test("PlaywrightRunner runs actions in an isolated context", async (t) => {
);
});

// TODO: Test accepts different browsers
test("PlaywrightRunner closes the browser after completing the job", async (t) => {
const { testAppURL } = t.context;

const runner = new PlaywrightRunner({
browser: JobBrowser.CHROMIUM,
steps: [
{
url: `${testAppURL}/`,
actions: ["page.waitForSelector('h1')"],
},
],
cookies: [],
options: [],
});

await runner.init();
await runner.exec();
await runner.finish();

t.assert(runner.browser === undefined);
});

test("PlaywrightRunner closes the browser if an error occurs", async (t) => {
const { testAppURL } = t.context;

const runner_1 = new PlaywrightRunner({
browser: JobBrowser.CHROMIUM,
steps: [
{
url: `${testAppURL}/`,
actions: ["page.on('invalid')", "page.waitForSelector('h1')"],
},
],
cookies: [],
options: [],
});

await t.throwsAsync(
async () => {
await runner_1.init();
await runner_1.exec();
await runner_1.finish();
},
{ message: "invalid action \"page.on('invalid')\"" },
);

t.assert(runner_1.browser === undefined);

const runner_2 = new PlaywrightRunner({
browser: JobBrowser.CHROMIUM,
steps: [
{
url: `${testAppURL}/`,
actions: ["page.waitForSelector('nonexistent', {timeout: 1000})"],
},
],
cookies: [],
options: [],
});

await t.throwsAsync(
async () => {
await runner_2.init();
await runner_2.exec();
await runner_2.finish();
},
{
message:
"invalid action \"page.waitForSelector('nonexistent', {timeout: 1000})\"",
},
);

t.assert(runner_2.browser === undefined);
});

0 comments on commit f305b6d

Please sign in to comment.