Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(browser): add Browser.BrowserType() method (#14468)

Sébastien Règne4c2fc6b6ebf8
diff --git a/docs/src/api/class-browser.md b/docs/src/api/class-browser.md
index 4549da135e..97d5da769b 100644
--- a/docs/src/api/class-browser.md
+++ b/docs/src/api/class-browser.md
@@ -79,6 +79,11 @@ Emitted when Browser gets disconnected from the browser application. This might
* Browser application is closed or crashed.
* The [`method: Browser.close`] method was called.
+## method: Browser.browserType
+- returns: <[BrowserType]>
+
+Get the browser type (chromium, firefox or webkit) that the browser belongs to.
+
## async method: Browser.close
In case this browser is obtained using [`method: BrowserType.launch`], closes the browser and all of its pages (if any
diff --git a/packages/playwright-core/src/client/browser.ts b/packages/playwright-core/src/client/browser.ts
index 38a22476ec..dcd507133e 100644
--- a/packages/playwright-core/src/client/browser.ts
+++ b/packages/playwright-core/src/client/browser.ts
@@ -56,6 +56,10 @@ export class Browser extends ChannelOwner<channels.BrowserChannel> implements ap
context._setBrowserType(browserType);
}
+ browserType(): BrowserType {
+ return this._browserType;
+ }
+
async newContext(options: BrowserContextOptions = {}): Promise<BrowserContext> {
options = { ...this._browserType._defaultContextOptions, ...options };
const contextOptions = await prepareBrowserContextParams(options);
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index 39b47d065f..64052e025b 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -12746,6 +12746,11 @@ export interface Browser extends EventEmitter {
*/
off(event: 'disconnected', listener: (browser: Browser) => void): this;
+ /**
+ * Get the browser type (chromium, firefox or webkit) that the browser belongs to.
+ */
+ browserType(): BrowserType;
+
/**
* In case this browser is obtained using
* [browserType.launch([options])](https://playwright.dev/docs/api/class-browsertype#browser-type-launch), closes the
diff --git a/tests/library/browser.spec.ts b/tests/library/browser.spec.ts
index da8d740175..df9d5c7b4b 100644
--- a/tests/library/browser.spec.ts
+++ b/tests/library/browser.spec.ts
@@ -16,6 +16,10 @@
import { browserTest as test, expect } from '../config/browserTest';
+test('should return browserType', function({ browser, browserType }) {
+ expect(browser.browserType()).toBe(browserType);
+});
+
test('should create new page @smoke', async function({ browser }) {
const page1 = await browser.newPage();
expect(browser.contexts().length).toBe(1);