feat: introduce BrowserType.name() (#732)

This helps a lot to produce nice logging:

```js
const { chromium, webkit } = require('playwright');

(async () => {
  for (const launcher of [chromium, webkit]) {
    console.log(`Testing on ${launcher.name()}`);
    const browser = await launcher.launch();
    // ...
    await browser.close();
  }
})();
```
This commit is contained in:
Andrey Lushnikov 2020-01-28 18:09:07 -08:00 committed by GitHub
parent 184b25ff7b
commit ce7c8d74b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 0 deletions

View File

@ -3402,6 +3402,7 @@ const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.
- [browserType.executablePath()](#browsertypeexecutablepath)
- [browserType.launch([options])](#browsertypelaunchoptions)
- [browserType.launchBrowserApp([options])](#browsertypelaunchbrowserappoptions)
- [browserType.name()](#browsertypename)
<!-- GEN:stop -->
#### browserType.connect(options)
@ -3523,6 +3524,11 @@ const browser = await chromium.launch({ // Or 'firefox' or 'webkit'.
- `devtools` <[boolean]> **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
- returns: <[Promise]<[BrowserApp]>> Promise which resolves to the browser app instance.
#### browserType.name()
- returns: <[string]>
Returns browser name. For example: `'chromium'`, `'webkit'` or `'firefox'`.
### class: ChromiumBrowser
* extends: [Browser]

View File

@ -41,6 +41,7 @@ export type LaunchOptions = BrowserArgOptions & {
export interface BrowserType {
executablePath(): string;
name(): string;
launchBrowserApp(options?: LaunchOptions): Promise<BrowserApp>;
launch(options?: LaunchOptions): Promise<Browser>;
defaultArgs(options?: BrowserArgOptions): string[];

View File

@ -43,6 +43,10 @@ export class Chromium implements BrowserType {
this._revision = preferredRevision;
}
name() {
return 'chromium';
}
async launch(options?: LaunchOptions): Promise<CRBrowser> {
const app = await this.launchBrowserApp(options);
const browser = await CRBrowser.connect(app.connectOptions());

View File

@ -42,6 +42,10 @@ export class Firefox implements BrowserType {
this._revision = preferredRevision;
}
name() {
return 'firefox';
}
async launch(options?: LaunchOptions): Promise<FFBrowser> {
const app = await this.launchBrowserApp(options);
const browser = await FFBrowser.connect(app.connectOptions());

View File

@ -47,6 +47,10 @@ export class WebKit implements BrowserType {
this._revision = preferredRevision;
}
name() {
return 'webkit';
}
async launch(options?: LaunchOptions): Promise<WKBrowser> {
const app = await this.launchBrowserApp(options);
const browser = await WKBrowser.connect(app.connectOptions());

View File

@ -81,6 +81,19 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
});
});
describe('Playwright.name', function() {
it('should work', async({server}) => {
if (WEBKIT)
expect(playwright.name()).toBe('webkit');
else if (FFOX)
expect(playwright.name()).toBe('firefox');
else if (CHROMIUM)
expect(playwright.name()).toBe('chromium');
else
throw new Error('Unknown browser');
});
});
describe('Playwright.defaultArguments', () => {
it('should return the default arguments', async() => {
if (CHROMIUM)