Skip to content

Commit

Permalink
feat: allow to pick stable channel (#5817)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Mar 13, 2021
1 parent 0d32b05 commit a96d6a7
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ jobs:
- run: xvfb-run --auto-servernum --server-args="-screen 0 1280x960x24" -- bash -c "ulimit -c unlimited && npx folio test/ --workers=1 --forbid-only --timeout=60000 --global-timeout=5400000 --retries=3 --reporter=dot,json -p video"
env:
BROWSER: "chromium"
CRPATH: "/opt/google/chrome/chrome"
PW_CHROMIUM_CHANNEL: "chrome"
FOLIO_JSON_OUTPUT_NAME: "test-results/report.json"
- uses: actions/upload-artifact@v1
if: ${{ always() }}
Expand Down
13 changes: 13 additions & 0 deletions docs/src/api/class-browsertype.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ Whether to run browser in headless mode. More details for
[Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode). Defaults to `true` unless the
[`option: devtools`] option is `true`.

### option: BrowserType.launch.channel
- `channel` <[string]>

Chromium distribution channel, one of
* chrome
* chrome-beta
* chrome-dev
* chrome-canary
* msedge
* msedge-beta
* msedge-dev
* msedge-canary

### option: BrowserType.launch.executablePath
- `executablePath` <[path]>

Expand Down
4 changes: 2 additions & 2 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ program

program
.command('install-deps [browserType...]')
.description('install dependencies necessary to run browser')
.description('install dependencies necessary to run browsers (will ask for sudo permissions)')
.action(async function(browserType) {
try {
await installDeps(browserType);
} catch (e) {
console.log(`Failed to install browsers\n${e}`);
console.log(`Failed to install browser dependencies\n${e}`);
process.exit(1);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/client/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class Connection {
return await new Promise((resolve, reject) => this._callbacks.set(id, { resolve, reject }));
} catch (e) {
const innerStack = ((process.env.PWDEBUGIMPL || isUnderTest()) && e.stack) ? e.stack.substring(e.stack.indexOf(e.message) + e.message.length) : '';
e.stack = e.message + innerStack + stack;
e.stack = e.message + innerStack + '\n' + stack;
throw e;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/protocol/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export interface BrowserTypeChannel extends Channel {
connectOverCDP(params: BrowserTypeConnectOverCDPParams, metadata?: Metadata): Promise<BrowserTypeConnectOverCDPResult>;
}
export type BrowserTypeLaunchParams = {
channel?: string,
executablePath?: string,
args?: string[],
ignoreAllDefaultArgs?: boolean,
Expand All @@ -240,6 +241,7 @@ export type BrowserTypeLaunchParams = {
slowMo?: number,
};
export type BrowserTypeLaunchOptions = {
channel?: string,
executablePath?: string,
args?: string[],
ignoreAllDefaultArgs?: boolean,
Expand Down
1 change: 1 addition & 0 deletions src/protocol/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ BrowserType:

launch:
parameters:
channel: string?
executablePath: string?
args:
type: array?
Expand Down
1 change: 1 addition & 0 deletions src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export function createScheme(tChannel: (name: string) => Validator): Scheme {
contentScript: tOptional(tBoolean),
});
scheme.BrowserTypeLaunchParams = tObject({
channel: tOptional(tString),
executablePath: tOptional(tString),
args: tOptional(tArray(tString)),
ignoreAllDefaultArgs: tOptional(tBoolean),
Expand Down
4 changes: 2 additions & 2 deletions src/server/browserType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export abstract class BrowserType extends SdkObject {
this._registry = playwrightOptions.registry;
}

executablePath(): string {
executablePath(options?: types.LaunchOptions): string {
return this._registry.executablePath(this._name) || '';
}

Expand Down Expand Up @@ -165,7 +165,7 @@ export abstract class BrowserType extends SdkObject {
else
browserArguments.push(...this._defaultArgs(options, isPersistent, userDataDir));

const executable = executablePath || this.executablePath();
const executable = executablePath || this.executablePath(options);
if (!executable)
throw new Error(`No executable path is specified. Pass "executablePath" option directly.`);
if (!(await existsAsync(executable))) {
Expand Down
7 changes: 7 additions & 0 deletions src/server/chromium/chromium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { ProgressController } from '../progress';
import { TimeoutSettings } from '../../utils/timeoutSettings';
import { helper } from '../helper';
import { CallMetadata } from '../instrumentation';
import { findChromiumChannel } from './findChromiumChannel';

export class Chromium extends BrowserType {
private _devtools: CRDevTools | undefined;
Expand All @@ -42,6 +43,12 @@ export class Chromium extends BrowserType {
this._devtools = this._createDevTools();
}

executablePath(options?: types.LaunchOptions): string {
if (options?.channel)
return findChromiumChannel(options.channel);
return super.executablePath(options);
}

async connectOverCDP(metadata: CallMetadata, wsEndpoint: string, options: { slowMo?: number, sdkLanguage: string }, timeout?: number) {
const controller = new ProgressController(metadata, this);
controller.setLogName('browser');
Expand Down
92 changes: 92 additions & 0 deletions src/server/chromium/findChromiumChannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import fs from 'fs';
import path from 'path';

function darwin(channel: string): string | undefined {
switch (channel) {
case 'chrome': return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
case 'chrome-canary': return '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary';
case 'msedge': return '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge';
case 'msedge-beta': return '/Applications/Microsoft Edge Beta.app/Contents/MacOS/Microsoft Edge Beta';
case 'msedge-dev': return '/Applications/Microsoft Edge Dev.app/Contents/MacOS/Microsoft Edge Dev';
case 'msedge-canary': return '/Applications/Microsoft Edge Canary.app/Contents/MacOS/Microsoft Edge Canary';
}
}

function linux(channel: string): string | undefined {
switch (channel) {
case 'chrome': return '/opt/google/chrome/chrome';
case 'chrome-beta': return '/opt/google/chrome-beta/chrome';
case 'chrome-dev': return '/opt/google/chrome-unstable/chrome';
case 'msedge-dev': return '/opt/microsoft/msedge-dev/msedge';
}
}

function win32(channel: string): string | undefined {
let suffix: string | undefined;
switch (channel) {
case 'chrome': suffix = `\\Google\\Chrome\\Application\\chrome.exe`; break;
case 'chrome-canary': suffix = `\\Google\\Chrome SxS\\Application\\chrome.exe`; break;
case 'msedge': suffix = `\\Microsoft\\Edge\\Application\\msedge.exe`; break;
case 'msedge-beta': suffix = `\\Microsoft\\Edge Beta\\Application\\msedge.exe`; break;
case 'msedge-dev': suffix = `\\Microsoft\\Edge Dev\\Application\\msedge.exe`; break;
case 'msedge-canary': suffix = `\\Microsoft\\Edge SxS\\Application\\msedge.exe`; break;
}
if (!suffix)
return;
const prefixes = [
process.env.LOCALAPPDATA, process.env.PROGRAMFILES, process.env['PROGRAMFILES(X86)']
].filter(Boolean) as string[];

let result: string | undefined;
prefixes.forEach(prefix => {
const chromePath = path.join(prefix, suffix!);
if (canAccess(chromePath))
result = chromePath;
});
return result;
}

function canAccess(file: string) {
if (!file)
return false;

try {
fs.accessSync(file);
return true;
} catch (e) {
return false;
}
}

export function findChromiumChannel(channel: string): string {
let result: string | undefined;
if (process.platform === 'linux')
result = linux(channel);
else if (process.platform === 'win32')
result = win32(channel);
else if (process.platform === 'darwin')
result = darwin(channel);

if (!result)
throw new Error(`Chromium distribution '${channel}' is not supported on ${process.platform}`);

if (canAccess(result))
return result;
throw new Error(`Chromium distribution was not found: ${channel}`);
}
1 change: 1 addition & 0 deletions src/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export type BrowserContextOptions = {
export type EnvArray = { name: string, value: string }[];

type LaunchOptionsBase = {
channel?: string,
executablePath?: string,
args?: string[],
ignoreDefaultArgs?: string[],
Expand Down
1 change: 1 addition & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fixtures.browserOptions.override(async ({ browserName, headful, slowMo }, run) =
if (executablePath)
console.error(`Using executable at ${executablePath}`);
await run({
channel: process.env.PW_CHROMIUM_CHANNEL,
executablePath,
handleSIGINT: false,
slowMo,
Expand Down
13 changes: 13 additions & 0 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10645,6 +10645,19 @@ export interface LaunchOptions {
*/
args?: Array<string>;

/**
* Chromium distribution channel, one of
* - chrome
* - chrome-beta
* - chrome-dev
* - chrome-canary
* - msedge
* - msedge-beta
* - msedge-dev
* - msedge-canary
*/
channel?: string;

/**
* Enable Chromium sandboxing. Defaults to `false`.
*/
Expand Down

0 comments on commit a96d6a7

Please sign in to comment.