chore: implement acceptInsecureCerts and version for BiDi (#10365)

This commit is contained in:
Alex Rudenko 2023-06-13 10:17:23 +02:00 committed by GitHub
parent 3c41b3ec78
commit 903afc3715
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 94 additions and 18 deletions

View File

@ -20,6 +20,6 @@ Promise<string>
## Remarks
For headless browser, this is similar to `HeadlessChrome/61.0.3153.0`. For non-headless, this is similar to `Chrome/61.0.3153.0`.
For headless browser, this is similar to `HeadlessChrome/61.0.3153.0`. For non-headless or new-headless, this is similar to `Chrome/61.0.3153.0`. For Firefox, it is similar to `Firefox/116.0a1`.
The format of browser.version() might change with future releases of browsers.

View File

@ -405,9 +405,11 @@ export class Browser extends EventEmitter {
* @remarks
*
* For headless browser, this is similar to `HeadlessChrome/61.0.3153.0`. For
* non-headless, this is similar to `Chrome/61.0.3153.0`.
* non-headless or new-headless, this is similar to `Chrome/61.0.3153.0`. For
* Firefox, it is similar to `Firefox/116.0a1`.
*
* The format of browser.version() might change with future releases of browsers.
* The format of browser.version() might change with future releases of
* browsers.
*/
version(): Promise<string> {
throw new Error('Not implemented');

View File

@ -29,24 +29,44 @@ import {Viewport} from '../PuppeteerViewport.js';
import {BrowserContext} from './BrowserContext.js';
import {Connection} from './Connection.js';
import {debugError} from './utils.js';
/**
* @internal
*/
export class Browser extends BrowserBase {
static readonly subscribeModules = ['browsingContext', 'network', 'log'];
#browserName = '';
#browserVersion = '';
static async create(opts: Options): Promise<Browser> {
let browserName = '';
let browserVersion = '';
// TODO: await until the connection is established.
try {
await opts.connection.send('session.new', {capabilities: {}});
} catch {}
const {result} = await opts.connection.send('session.new', {
capabilities: {
alwaysMatch: {
acceptInsecureCerts: opts.ignoreHTTPSErrors,
},
},
});
browserName = result.capabilities.browserName ?? '';
browserVersion = result.capabilities.browserVersion ?? '';
} catch (err) {
debugError(err);
}
await opts.connection.send('session.subscribe', {
events: Browser.subscribeModules as Bidi.Message.EventNames[],
});
return new Browser(opts);
return new Browser({
...opts,
browserName,
browserVersion,
});
}
#process?: ChildProcess;
@ -54,12 +74,19 @@ export class Browser extends BrowserBase {
#connection: Connection;
#defaultViewport: Viewport | null;
constructor(opts: Options) {
constructor(
opts: Options & {
browserName: string;
browserVersion: string;
}
) {
super();
this.#process = opts.process;
this.#closeCallback = opts.closeCallback;
this.#connection = opts.connection;
this.#defaultViewport = opts.defaultViewport;
this.#browserName = opts.browserName;
this.#browserVersion = opts.browserVersion;
this.#process?.on('close', () => {
return this.emit(BrowserEmittedEvents.Disconnected);
@ -90,6 +117,10 @@ export class Browser extends BrowserBase {
defaultViewport: this.#defaultViewport,
});
}
override async version(): Promise<string> {
return `${this.#browserName}/${this.#browserVersion}`;
}
}
interface Options {
@ -97,4 +128,5 @@ interface Options {
closeCallback?: BrowserCloseCallback;
connection: Connection;
defaultViewport: Viewport | null;
ignoreHTTPSErrors?: boolean;
}

View File

@ -26,6 +26,29 @@ import {BrowsingContext} from './BrowsingContext.js';
const debugProtocolSend = debug('puppeteer:webDriverBiDi:SEND ►');
const debugProtocolReceive = debug('puppeteer:webDriverBiDi:RECV ◀');
type Capability = {
// session.CapabilityRequest = {
// ? acceptInsecureCerts: bool,
// ? browserName: text,
// ? browserVersion: text,
// ? platformName: text,
// ? proxy: {
// ? proxyType: "pac" / "direct" / "autodetect" / "system" / "manual",
// ? proxyAutoconfigUrl: text,
// ? ftpProxy: text,
// ? httpProxy: text,
// ? noProxy: [*text],
// ? sslProxy: text,
// ? socksProxy: text,
// ? socksVersion: 0..255,
// },
// Extensible
// };
acceptInsecureCerts?: boolean;
browserName?: string;
browserVersion?: string;
};
/**
* @internal
*/
@ -73,8 +96,19 @@ interface Commands {
};
'session.new': {
params: {capabilities?: Record<any, unknown>}; // TODO: Update Types in chromium bidi
returnType: {sessionId: string};
params: {
// capabilities: session.CapabilitiesRequest
capabilities?: {
// session.CapabilitiesRequest = {
// ? alwaysMatch: session.CapabilityRequest,
// ? firstMatch: [*session.CapabilityRequest]
// }
alwaysMatch?: Capability;
};
}; // TODO: Update Types in chromium bidi
returnType: {
result: {sessionId: string; capabilities: Capability};
};
};
'session.status': {
params: object;

View File

@ -143,6 +143,7 @@ export class ProductLauncher {
protocolTimeout,
slowMo,
defaultViewport,
ignoreHTTPSErrors,
}
);
} else {
@ -169,6 +170,7 @@ export class ProductLauncher {
protocolTimeout,
slowMo,
defaultViewport,
ignoreHTTPSErrors,
}
);
} else {
@ -329,6 +331,7 @@ export class ProductLauncher {
protocolTimeout: number | undefined;
slowMo: number;
defaultViewport: Viewport | null;
ignoreHTTPSErrors?: boolean;
}
): Promise<Browser> {
// TODO: use other options too.
@ -341,6 +344,7 @@ export class ProductLauncher {
closeCallback,
process: browserProcess.nodeProcess,
defaultViewport: opts.defaultViewport,
ignoreHTTPSErrors: opts.ignoreHTTPSErrors,
});
}
@ -355,6 +359,7 @@ export class ProductLauncher {
protocolTimeout: number | undefined;
slowMo: number;
defaultViewport: Viewport | null;
ignoreHTTPSErrors?: boolean;
}
): Promise<Browser> {
const browserWSEndpoint =
@ -377,6 +382,7 @@ export class ProductLauncher {
closeCallback,
process: browserProcess.nodeProcess,
defaultViewport: opts.defaultViewport,
ignoreHTTPSErrors: opts.ignoreHTTPSErrors,
});
}

View File

@ -269,6 +269,12 @@
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[browser.spec] Browser specs Browser.version should return version",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["firefox", "webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[chromiumonly.spec] *",
"platforms": ["darwin", "linux", "win32"],

View File

@ -37,12 +37,10 @@ describe('WebDriver BiDi', () => {
const transport = new TestConnectionTransport();
const connection = new Connection(transport);
const responsePromise = connection.send('session.new', {
capabilities: {
proxy: {},
},
capabilities: {},
});
expect(transport.sent).toEqual([
`{"id":1,"method":"session.new","params":{"capabilities":{"proxy":{}}}}`,
`{"id":1,"method":"session.new","params":{"capabilities":{}}}`,
]);
const id = JSON.parse(transport.sent[0]!).id;
const rawResponse = {

View File

@ -22,14 +22,12 @@ describe('Browser specs', function () {
setupTestBrowserHooks();
describe('Browser.version', function () {
it('should return whether we are in headless', async () => {
const {browser, isHeadless, headless} = getTestState();
it('should return version', async () => {
const {browser} = getTestState();
const version = await browser.version();
expect(version.length).toBeGreaterThan(0);
expect(version.startsWith('Headless')).toBe(
isHeadless && headless !== 'new'
);
expect(version.toLowerCase()).atLeastOneToContain(['firefox', 'chrome']);
});
});