From 0533764aac6353548ba8e1bc9d2f1f8921f7f8af Mon Sep 17 00:00:00 2001 From: Maksim Sadym <69349599+sadym-chromium@users.noreply.github.com> Date: Fri, 10 Nov 2023 13:55:08 +0100 Subject: [PATCH] test: pass `protocol` in `Puppeteer.connect` in tests (#11342) Add an internal field protocol: 'cdp' | 'webDriverBiDi' to the Browser and use is in tests when Puppeter.connect(...) is called. Co-authored-by: Maksim Sadym --- packages/puppeteer-core/src/api/Browser.ts | 5 +++++ packages/puppeteer-core/src/bidi/Browser.ts | 2 ++ packages/puppeteer-core/src/cdp/Browser.ts | 2 ++ test/TestExpectations.json | 16 ++++++++-------- test/src/browser.spec.ts | 2 ++ test/src/browsercontext.spec.ts | 1 + test/src/launcher.spec.ts | 17 ++++++++++++++++- 7 files changed, 36 insertions(+), 9 deletions(-) diff --git a/packages/puppeteer-core/src/api/Browser.ts b/packages/puppeteer-core/src/api/Browser.ts index 1dba35dc..752c52ea 100644 --- a/packages/puppeteer-core/src/api/Browser.ts +++ b/packages/puppeteer-core/src/api/Browser.ts @@ -437,4 +437,9 @@ export abstract class Browser extends EventEmitter { [asyncDisposeSymbol](): Promise { return this.close(); } + + /** + * @internal + */ + abstract get protocol(): 'cdp' | 'webDriverBiDi'; } diff --git a/packages/puppeteer-core/src/bidi/Browser.ts b/packages/puppeteer-core/src/bidi/Browser.ts index a796d622..e426f75b 100644 --- a/packages/puppeteer-core/src/bidi/Browser.ts +++ b/packages/puppeteer-core/src/bidi/Browser.ts @@ -57,6 +57,8 @@ export interface BidiBrowserOptions { * @internal */ export class BidiBrowser extends Browser { + readonly protocol = 'webDriverBiDi'; + // TODO: Update generator to include fully module static readonly subscribeModules: string[] = [ 'browsingContext', diff --git a/packages/puppeteer-core/src/cdp/Browser.ts b/packages/puppeteer-core/src/cdp/Browser.ts index 037053f3..c61bd766 100644 --- a/packages/puppeteer-core/src/cdp/Browser.ts +++ b/packages/puppeteer-core/src/cdp/Browser.ts @@ -53,6 +53,8 @@ import {TargetManagerEvent, type TargetManager} from './TargetManager.js'; * @internal */ export class CdpBrowser extends BrowserBase { + readonly protocol = 'cdp'; + static async _create( product: 'firefox' | 'chrome' | undefined, connection: Connection, diff --git a/test/TestExpectations.json b/test/TestExpectations.json index 0918e124..0c118e9e 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -381,13 +381,13 @@ "testIdPattern": "[browser.spec] Browser specs Browser.isConnected should set the browser connected state", "platforms": ["darwin", "linux", "win32"], "parameters": ["webDriverBiDi"], - "expectations": ["PASS"] + "expectations": ["FAIL"] }, { "testIdPattern": "[browser.spec] Browser specs Browser.process should not return child_process for remote browser", "platforms": ["darwin", "linux", "win32"], "parameters": ["webDriverBiDi"], - "expectations": ["PASS"] + "expectations": ["FAIL"] }, { "testIdPattern": "[browser.spec] Browser specs Browser.process should return child_process instance", @@ -2031,19 +2031,19 @@ "testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.connect should be able to close remote browser", "platforms": ["darwin", "linux", "win32"], "parameters": ["chrome", "webDriverBiDi"], - "expectations": ["PASS"] + "expectations": ["FAIL"] }, { "testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.connect should be able to connect multiple times to the same browser", "platforms": ["darwin", "linux", "win32"], "parameters": ["chrome", "webDriverBiDi"], - "expectations": ["PASS"] + "expectations": ["FAIL"] }, { "testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.connect should be able to connect to a browser with no page targets", "platforms": ["darwin", "linux", "win32"], "parameters": ["chrome", "webDriverBiDi"], - "expectations": ["PASS"] + "expectations": ["FAIL"] }, { "testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.connect should be able to connect to a browser with no page targets", @@ -2061,7 +2061,7 @@ "testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.connect should be able to connect to the same page simultaneously", "platforms": ["darwin", "linux", "win32"], "parameters": ["chrome", "webDriverBiDi"], - "expectations": ["PASS"] + "expectations": ["FAIL"] }, { "testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.connect should be able to reconnect to a disconnected browser", @@ -2073,13 +2073,13 @@ "testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.connect should support ignoreHTTPSErrors option", "platforms": ["darwin", "linux", "win32"], "parameters": ["chrome", "webDriverBiDi"], - "expectations": ["PASS"] + "expectations": ["FAIL"] }, { "testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.connect should support targetFilter option", "platforms": ["darwin", "linux", "win32"], "parameters": ["chrome", "webDriverBiDi"], - "expectations": ["PASS"] + "expectations": ["FAIL"] }, { "testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.connect should support targetFilter option", diff --git a/test/src/browser.spec.ts b/test/src/browser.spec.ts index 32b5bf84..842b794b 100644 --- a/test/src/browser.spec.ts +++ b/test/src/browser.spec.ts @@ -67,6 +67,7 @@ describe('Browser specs', function () { const browserWSEndpoint = browser.wsEndpoint(); const remoteBrowser = await puppeteer.connect({ browserWSEndpoint, + protocol: browser.protocol, }); expect(remoteBrowser.process()).toBe(null); remoteBrowser.disconnect(); @@ -80,6 +81,7 @@ describe('Browser specs', function () { const browserWSEndpoint = browser.wsEndpoint(); const newBrowser = await puppeteer.connect({ browserWSEndpoint, + protocol: browser.protocol, }); expect(newBrowser.isConnected()).toBe(true); newBrowser.disconnect(); diff --git a/test/src/browsercontext.spec.ts b/test/src/browsercontext.spec.ts index 9ae0fefc..c3080e29 100644 --- a/test/src/browsercontext.spec.ts +++ b/test/src/browsercontext.spec.ts @@ -226,6 +226,7 @@ describe('BrowserContext', function () { expect(browser.browserContexts()).toHaveLength(2); const remoteBrowser = await puppeteer.connect({ browserWSEndpoint: browser.wsEndpoint(), + protocol: browser.protocol, }); const contexts = remoteBrowser.browserContexts(); expect(contexts).toHaveLength(2); diff --git a/test/src/launcher.spec.ts b/test/src/launcher.spec.ts index a84d4c53..c828c022 100644 --- a/test/src/launcher.spec.ts +++ b/test/src/launcher.spec.ts @@ -43,6 +43,7 @@ describe('Launcher specs', function () { try { const remote = await puppeteer.connect({ browserWSEndpoint: browser.wsEndpoint(), + protocol: browser.protocol, }); const page = await remote.newPage(); const navigationPromise = page @@ -69,6 +70,7 @@ describe('Launcher specs', function () { try { const remote = await puppeteer.connect({ browserWSEndpoint: browser.wsEndpoint(), + protocol: browser.protocol, }); const page = await remote.newPage(); const watchdog = page @@ -90,6 +92,7 @@ describe('Launcher specs', function () { try { const remote = await puppeteer.connect({ browserWSEndpoint: browser.wsEndpoint(), + protocol: browser.protocol, }); const newPage = await remote.newPage(); const results = await Promise.all([ @@ -624,6 +627,7 @@ describe('Launcher specs', function () { try { const otherBrowser = await puppeteer.connect({ browserWSEndpoint: browser.wsEndpoint(), + protocol: browser.protocol, }); const page = await otherBrowser.newPage(); expect( @@ -648,6 +652,7 @@ describe('Launcher specs', function () { try { const remoteBrowser = await puppeteer.connect({ browserWSEndpoint: browser.wsEndpoint(), + protocol: browser.protocol, }); await Promise.all([ waitEvent(browser, 'disconnected'), @@ -669,6 +674,7 @@ describe('Launcher specs', function () { ); const remoteBrowser = await puppeteer.connect({ browserWSEndpoint: browser.wsEndpoint(), + protocol: browser.protocol, }); await Promise.all([ waitEvent(browser, 'disconnected'), @@ -691,6 +697,7 @@ describe('Launcher specs', function () { const remoteBrowser = await puppeteer.connect({ browserWSEndpoint, ignoreHTTPSErrors: true, + protocol: browser.protocol, }); const page = await remoteBrowser.newPage(); let error!: Error; @@ -758,6 +765,7 @@ describe('Launcher specs', function () { targetFilter: target => { return !target.url().includes('should-be-ignored'); }, + protocol: browser.protocol, }); const pages = await remoteBrowser.pages(); @@ -786,7 +794,10 @@ describe('Launcher specs', function () { await page.goto(server.PREFIX + '/frames/nested-frames.html'); browser.disconnect(); - const remoteBrowser = await puppeteer.connect({browserWSEndpoint}); + const remoteBrowser = await puppeteer.connect({ + browserWSEndpoint, + protocol: browser.protocol, + }); const pages = await remoteBrowser.pages(); const restoredPage = pages.find(page => { return page.url() === server.PREFIX + '/frames/nested-frames.html'; @@ -815,6 +826,7 @@ describe('Launcher specs', function () { try { const browserTwo = await puppeteer.connect({ browserWSEndpoint: browserOne.wsEndpoint(), + protocol: browserOne.protocol, }); const [page1, page2] = await Promise.all([ new Promise(x => { @@ -854,6 +866,7 @@ describe('Launcher specs', function () { const browserTwo = await puppeteer.connect({ browserWSEndpoint, + protocol: browserOne.protocol, }); const pages = await browserTwo.pages(); const pageTwo = pages.find(page => { @@ -951,9 +964,11 @@ describe('Launcher specs', function () { const browserWSEndpoint = browser.wsEndpoint(); const remoteBrowser1 = await puppeteer.connect({ browserWSEndpoint, + protocol: browser.protocol, }); const remoteBrowser2 = await puppeteer.connect({ browserWSEndpoint, + protocol: browser.protocol, }); let disconnectedOriginal = 0;