Skip to content

Commit 585b251

Browse files
add respawn on pupetter
1 parent 0c251da commit 585b251

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

testing/testing-screenshot/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ export class Worker {
4040
return this.browser;
4141
}
4242

43+
async relaunch() {
44+
await this.close();
45+
return this.launch();
46+
}
47+
4348
async screenshot({ htmlcss, viewport }: ScreenshotOptions) {
49+
// check if page is available (try to avoid TargetClosedError)
50+
if (!this.page || this.page.isClosed()) {
51+
await this.relaunch();
52+
}
53+
4454
this.page.setViewport(viewport);
4555
await this.page.setContent(htmlcss, { waitUntil: "networkidle0" });
4656
const buffer = await this.page.screenshot({
@@ -52,7 +62,11 @@ export class Worker {
5262
}
5363

5464
async close() {
55-
await this.browser.close();
65+
try {
66+
await this.browser.close();
67+
} catch (e) {}
68+
this.browser = null;
69+
this.page = null;
5670
}
5771

5872
terminate() {

0 commit comments

Comments
 (0)