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

fix: add getPuppeteer timeout #1005

Merged
merged 1 commit into from
Aug 28, 2024
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
45 changes: 43 additions & 2 deletions src/browser/commands/getPuppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,51 @@

const urljoin = require("url-join");

const PUPPETEER_REJECT_TIMEOUT = 5000;

module.exports.default = browser => {
const { publicAPI: session, config } = browser;

if (!config.browserWSEndpoint || !session.getPuppeteer) {
if (!session.getPuppeteer) {
return;
}

session.overwriteCommand("getPuppeteer", async origGetPuppeteer => {
return new Promise((resolve, reject) => {
let isSettled = false;
let rejectTimeout;

origGetPuppeteer()
.then(puppeteer => {
if (!isSettled) {
resolve(puppeteer);
}
})
.catch(error => {
if (!isSettled) {
reject(error);
}
})
.finally(() => {
isSettled = true;
clearTimeout(rejectTimeout);
});

rejectTimeout = setTimeout(() => {
if (isSettled) {
return;
}

isSettled = true;

browser.markAsBroken();

reject(new Error(`Unable to establish a CDP connection in ${PUPPETEER_REJECT_TIMEOUT} ms`));
}, PUPPETEER_REJECT_TIMEOUT);
});
});

if (!config.browserWSEndpoint) {
return;
}

Expand All @@ -17,7 +58,7 @@ module.exports.default = browser => {
setCdpEndpoint(session.capabilities, newBrowserWSEndpoint);

try {
return origGetPuppeteer();
return await origGetPuppeteer();
} finally {
setCdpEndpoint(session.capabilities, prevBrowserWSEndpoint);
}
Expand Down
24 changes: 12 additions & 12 deletions test/src/browser/commands/getPuppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,21 @@ describe('"getPuppeteer" command', () => {
return browser.init({ sessionId: session.sessionId, sessionCaps: session.capabilities });
};

describe("should not overwrite command if", () => {
it('"browserWSEndpoint" is not specified', async () => {
const session = mkSessionStub_();
it('should not overwrite command if command "getPuppeteer" does not exist in browser', async () => {
const session = mkSessionStub_();
session.getPuppeteer = undefined;

await initBrowser_({ session });
await initBrowser_();

assert.neverCalledWith(session.overwriteCommand, "getPuppeteer");
});
assert.neverCalledWith(session.overwriteCommand, "getPuppeteer");
});

it('command "getPuppeteer" does not exist in browser', async () => {
const session = mkSessionStub_();
session.getPuppeteer = undefined;
it('should only overwrite timeouts wrapper if "browserWSEndpoint" is not specified', async () => {
const session = mkSessionStub_();

await initBrowser_();
await initBrowser_({ session });

assert.neverCalledWith(session.overwriteCommand, "getPuppeteer");
});
assert.equal(session.overwriteCommand.withArgs("getPuppeteer").callCount, 1);
});

it("should overwrite command", async () => {
Expand Down Expand Up @@ -74,6 +72,8 @@ describe('"getPuppeteer" command', () => {

origGetPuppeteerFn.callsFake(() => {
capsBeforeReset = _.cloneDeep(session.capabilities);

return Promise.resolve();
});

const browser = mkBrowser_({ browserWSEndpoint: "ws://new.endpoint/devtools" });
Expand Down
Loading