Skip to content

Commit

Permalink
feat: introduce BrowserType.name() (#732)
Browse files Browse the repository at this point in the history
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();
  }
})();
```
  • Loading branch information
aslushnikov authored Jan 29, 2020
1 parent 184b25f commit ce7c8d7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions src/server/browserType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
4 changes: 4 additions & 0 deletions src/server/chromium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions src/server/firefox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions src/server/webkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
13 changes: 13 additions & 0 deletions test/launcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ce7c8d7

Please sign in to comment.