class Playwright::Browser
-
extends: [EventEmitter]
A Browser
is created via [`method: BrowserType.launch
`]. An example of using a `Browser` to create a `Page`:
“`python sync from playwright.sync_api import sync_playwright
def run(playwright):
firefox = playwright.firefox browser = firefox.launch() page = browser.new_page() page.goto("https://example.com") browser.close()
with sync_playwright() as playwright:
run(playwright)
“`
Public Instance Methods
In case this browser is obtained using [`method: BrowserType.launch
`], closes the browser and all of its pages (if any were opened).
In case this browser is connected to, clears all created contexts belonging to this browser and disconnects from the browser server.
The `Browser` object itself is considered to be disposed and cannot be used anymore.
# File lib/playwright_api/browser.rb, line 28 def close wrap_impl(@impl.close) end
Indicates that the browser is connected.
# File lib/playwright_api/browser.rb, line 45 def connected? wrap_impl(@impl.connected?) end
Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.
“`python sync browser = pw.webkit.launch() print(len(browser.contexts())) # prints `0` context = browser.new_context() print(len(browser.contexts())) # prints `1` “`
# File lib/playwright_api/browser.rb, line 40 def contexts wrap_impl(@impl.contexts) end
> NOTE: CDP Sessions are only supported on Chromium-based browsers.
Returns the newly created browser session.
# File lib/playwright_api/browser.rb, line 52 def new_browser_cdp_session wrap_impl(@impl.new_browser_cdp_session) end
Creates a new browser context. It won't share cookies/cache with other browser contexts.
“`python sync browser = playwright.firefox.launch() # or “chromium” or “webkit”. # create a new incognito browser context. context = browser.new_context() # create a new page in a pristine context. page = context.new_page() page.goto(“example.com”) “`
# File lib/playwright_api/browser.rb, line 66 def new_context( acceptDownloads: nil, baseURL: nil, bypassCSP: nil, colorScheme: nil, deviceScaleFactor: nil, extraHTTPHeaders: nil, geolocation: nil, hasTouch: nil, httpCredentials: nil, ignoreHTTPSErrors: nil, isMobile: nil, javaScriptEnabled: nil, locale: nil, noViewport: nil, offline: nil, permissions: nil, proxy: nil, record_har_omit_content: nil, record_har_path: nil, record_video_dir: nil, record_video_size: nil, reducedMotion: nil, screen: nil, storageState: nil, timezoneId: nil, userAgent: nil, viewport: nil, &block) wrap_impl(@impl.new_context(acceptDownloads: unwrap_impl(acceptDownloads), baseURL: unwrap_impl(baseURL), bypassCSP: unwrap_impl(bypassCSP), colorScheme: unwrap_impl(colorScheme), deviceScaleFactor: unwrap_impl(deviceScaleFactor), extraHTTPHeaders: unwrap_impl(extraHTTPHeaders), geolocation: unwrap_impl(geolocation), hasTouch: unwrap_impl(hasTouch), httpCredentials: unwrap_impl(httpCredentials), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), isMobile: unwrap_impl(isMobile), javaScriptEnabled: unwrap_impl(javaScriptEnabled), locale: unwrap_impl(locale), noViewport: unwrap_impl(noViewport), offline: unwrap_impl(offline), permissions: unwrap_impl(permissions), proxy: unwrap_impl(proxy), record_har_omit_content: unwrap_impl(record_har_omit_content), record_har_path: unwrap_impl(record_har_path), record_video_dir: unwrap_impl(record_video_dir), record_video_size: unwrap_impl(record_video_size), reducedMotion: unwrap_impl(reducedMotion), screen: unwrap_impl(screen), storageState: unwrap_impl(storageState), timezoneId: unwrap_impl(timezoneId), userAgent: unwrap_impl(userAgent), viewport: unwrap_impl(viewport), &wrap_block_call(block))) end
Creates a new page in a new browser context. Closing this page will close the context as well.
This is a convenience API that should only be used for the single-page scenarios and short snippets. Production code and testing frameworks should explicitly create [`method: Browser.newContext`] followed by the
- `method: BrowserContext.newPage`
-
to control their exact life times.
# File lib/playwright_api/browser.rb, line 103 def new_page( acceptDownloads: nil, baseURL: nil, bypassCSP: nil, colorScheme: nil, deviceScaleFactor: nil, extraHTTPHeaders: nil, geolocation: nil, hasTouch: nil, httpCredentials: nil, ignoreHTTPSErrors: nil, isMobile: nil, javaScriptEnabled: nil, locale: nil, noViewport: nil, offline: nil, permissions: nil, proxy: nil, record_har_omit_content: nil, record_har_path: nil, record_video_dir: nil, record_video_size: nil, reducedMotion: nil, screen: nil, storageState: nil, timezoneId: nil, userAgent: nil, viewport: nil, &block) wrap_impl(@impl.new_page(acceptDownloads: unwrap_impl(acceptDownloads), baseURL: unwrap_impl(baseURL), bypassCSP: unwrap_impl(bypassCSP), colorScheme: unwrap_impl(colorScheme), deviceScaleFactor: unwrap_impl(deviceScaleFactor), extraHTTPHeaders: unwrap_impl(extraHTTPHeaders), geolocation: unwrap_impl(geolocation), hasTouch: unwrap_impl(hasTouch), httpCredentials: unwrap_impl(httpCredentials), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), isMobile: unwrap_impl(isMobile), javaScriptEnabled: unwrap_impl(javaScriptEnabled), locale: unwrap_impl(locale), noViewport: unwrap_impl(noViewport), offline: unwrap_impl(offline), permissions: unwrap_impl(permissions), proxy: unwrap_impl(proxy), record_har_omit_content: unwrap_impl(record_har_omit_content), record_har_path: unwrap_impl(record_har_path), record_video_dir: unwrap_impl(record_video_dir), record_video_size: unwrap_impl(record_video_size), reducedMotion: unwrap_impl(reducedMotion), screen: unwrap_impl(screen), storageState: unwrap_impl(storageState), timezoneId: unwrap_impl(timezoneId), userAgent: unwrap_impl(userAgent), viewport: unwrap_impl(viewport), &wrap_block_call(block))) end
– inherited from EventEmitter
– @nodoc
# File lib/playwright_api/browser.rb, line 179 def off(event, callback) event_emitter_proxy.off(event, callback) end
– inherited from EventEmitter
– @nodoc
# File lib/playwright_api/browser.rb, line 173 def on(event, callback) event_emitter_proxy.on(event, callback) end
– inherited from EventEmitter
– @nodoc
# File lib/playwright_api/browser.rb, line 167 def once(event, callback) event_emitter_proxy.once(event, callback) end
> NOTE: This API controls [Chromium Tracing](www.chromium.org/developers/how-tos/trace-event-profiling-tool) which is a low-level chromium-specific debugging tool. API to control [Playwright Tracing](../trace-viewer) could be found [here](./class-tracing).
You can use [`method: Browser.startTracing`] and [`method: Browser.stopTracing`] to create a trace file that can be opened in Chrome DevTools performance panel.
“`python sync browser.start_tracing(page, path=“trace.json”) page.goto(“www.google.com”) browser.stop_tracing() “`
# File lib/playwright_api/browser.rb, line 147 def start_tracing(page: nil, categories: nil, path: nil, screenshots: nil) wrap_impl(@impl.start_tracing(page: unwrap_impl(page), categories: unwrap_impl(categories), path: unwrap_impl(path), screenshots: unwrap_impl(screenshots))) end
> NOTE: This API controls [Chromium Tracing](www.chromium.org/developers/how-tos/trace-event-profiling-tool) which is a low-level chromium-specific debugging tool. API to control [Playwright Tracing](../trace-viewer) could be found [here](./class-tracing).
Returns the buffer with trace data.
# File lib/playwright_api/browser.rb, line 156 def stop_tracing wrap_impl(@impl.stop_tracing) end
Returns the browser version.
# File lib/playwright_api/browser.rb, line 161 def version wrap_impl(@impl.version) end
Private Instance Methods
# File lib/playwright_api/browser.rb, line 183 def event_emitter_proxy @event_emitter_proxy ||= EventEmitterProxy.new(self, @impl) end