Skip to content

Commit ce7c8d7

Browse files
authored
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(); } })(); ```
1 parent 184b25f commit ce7c8d7

File tree

6 files changed

+32
-0
lines changed

6 files changed

+32
-0
lines changed

docs/api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,6 +3402,7 @@ const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.
34023402
- [browserType.executablePath()](#browsertypeexecutablepath)
34033403
- [browserType.launch([options])](#browsertypelaunchoptions)
34043404
- [browserType.launchBrowserApp([options])](#browsertypelaunchbrowserappoptions)
3405+
- [browserType.name()](#browsertypename)
34053406
<!-- GEN:stop -->
34063407

34073408
#### browserType.connect(options)
@@ -3523,6 +3524,11 @@ const browser = await chromium.launch({ // Or 'firefox' or 'webkit'.
35233524
- `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`.
35243525
- returns: <[Promise]<[BrowserApp]>> Promise which resolves to the browser app instance.
35253526

3527+
#### browserType.name()
3528+
- returns: <[string]>
3529+
3530+
Returns browser name. For example: `'chromium'`, `'webkit'` or `'firefox'`.
3531+
35263532
### class: ChromiumBrowser
35273533

35283534
* extends: [Browser]

src/server/browserType.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type LaunchOptions = BrowserArgOptions & {
4141

4242
export interface BrowserType {
4343
executablePath(): string;
44+
name(): string;
4445
launchBrowserApp(options?: LaunchOptions): Promise<BrowserApp>;
4546
launch(options?: LaunchOptions): Promise<Browser>;
4647
defaultArgs(options?: BrowserArgOptions): string[];

src/server/chromium.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export class Chromium implements BrowserType {
4343
this._revision = preferredRevision;
4444
}
4545

46+
name() {
47+
return 'chromium';
48+
}
49+
4650
async launch(options?: LaunchOptions): Promise<CRBrowser> {
4751
const app = await this.launchBrowserApp(options);
4852
const browser = await CRBrowser.connect(app.connectOptions());

src/server/firefox.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export class Firefox implements BrowserType {
4242
this._revision = preferredRevision;
4343
}
4444

45+
name() {
46+
return 'firefox';
47+
}
48+
4549
async launch(options?: LaunchOptions): Promise<FFBrowser> {
4650
const app = await this.launchBrowserApp(options);
4751
const browser = await FFBrowser.connect(app.connectOptions());

src/server/webkit.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export class WebKit implements BrowserType {
4747
this._revision = preferredRevision;
4848
}
4949

50+
name() {
51+
return 'webkit';
52+
}
53+
5054
async launch(options?: LaunchOptions): Promise<WKBrowser> {
5155
const app = await this.launchBrowserApp(options);
5256
const browser = await WKBrowser.connect(app.connectOptions());

test/launcher.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
8181
});
8282
});
8383

84+
describe('Playwright.name', function() {
85+
it('should work', async({server}) => {
86+
if (WEBKIT)
87+
expect(playwright.name()).toBe('webkit');
88+
else if (FFOX)
89+
expect(playwright.name()).toBe('firefox');
90+
else if (CHROMIUM)
91+
expect(playwright.name()).toBe('chromium');
92+
else
93+
throw new Error('Unknown browser');
94+
});
95+
});
96+
8497
describe('Playwright.defaultArguments', () => {
8598
it('should return the default arguments', async() => {
8699
if (CHROMIUM)

0 commit comments

Comments
 (0)