Skip to content

Commit

Permalink
Merge pull request #1005 from gemini-testing/HERMIONE-1360.if
Browse files Browse the repository at this point in the history
fix: add getPuppeteer timeout
  • Loading branch information
KuznetsovRoman authored Aug 28, 2024
2 parents 6181370 + a2d8e2e commit f438e44
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
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

0 comments on commit f438e44

Please sign in to comment.