Class: Playwright::Browser
- Inherits:
-
PlaywrightApi
- Object
- PlaywrightApi
- Playwright::Browser
- Defined in:
- lib/playwright_api/browser.rb,
sig/playwright.rbs
Overview
A Browser is created via [method: BrowserType.launch]. An example of using a Browser to create a Page:
from playwright.sync_api import sync_playwright, Playwright
def run(playwright: 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)
Instance Method Summary collapse
-
#bind(title, host: nil, port: nil, workspaceDir: nil) ⇒ Hash[untyped, untyped]
Binds the browser to a named pipe or web socket, making it available for other clients to connect to.
-
#browser_type ⇒ BrowserType
Get the browser type (chromium, firefox or webkit) that the browser belongs to.
-
#close(reason: nil) ⇒ void
In case this browser is obtained using [
method: BrowserType.launch], closes the browser and all of its pages (if any were opened). -
#connected? ⇒ Boolean
Indicates that the browser is connected.
-
#contexts ⇒ Array[untyped]
Returns an array of all open browser contexts.
-
#new_browser_cdp_session ⇒ CDPSession
NOTE: CDP Sessions are only supported on Chromium-based browsers.
-
#new_context(acceptDownloads: nil, baseURL: nil, bypassCSP: nil, clientCertificates: nil, colorScheme: nil, contrast: nil, deviceScaleFactor: nil, extraHTTPHeaders: nil, forcedColors: 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_content: nil, record_har_mode: nil, record_har_omit_content: nil, record_har_path: nil, record_har_url_filter: nil, record_video_dir: nil, record_video_fps: nil, record_video_size: nil, reducedMotion: nil, screen: nil, serviceWorkers: nil, storageState: nil, strictSelectors: nil, timezoneId: nil, userAgent: nil, viewport: nil) {|arg0| ... } ⇒ BrowserContext
Creates a new browser context.
-
#new_page(acceptDownloads: nil, baseURL: nil, bypassCSP: nil, clientCertificates: nil, colorScheme: nil, contrast: nil, deviceScaleFactor: nil, extraHTTPHeaders: nil, forcedColors: 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_content: nil, record_har_mode: nil, record_har_omit_content: nil, record_har_path: nil, record_har_url_filter: nil, record_video_dir: nil, record_video_fps: nil, record_video_size: nil, reducedMotion: nil, screen: nil, serviceWorkers: nil, storageState: nil, strictSelectors: nil, timezoneId: nil, userAgent: nil, viewport: nil) {|arg0| ... } ⇒ Page
Creates a new page in a new browser context.
-
#off(event, callback) ⇒ Object
-- inherited from EventEmitter --.
-
#on(event, callback) ⇒ Object
-- inherited from EventEmitter --.
-
#once(event, callback) ⇒ Object
-- inherited from EventEmitter --.
-
#start_tracing(categories: nil, page: nil, path: nil, screenshots: nil) ⇒ void
NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool.
-
#stop_tracing ⇒ String
NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool.
-
#unbind ⇒ void
Unbinds the browser server previously bound with [
method: Browser.bind]. -
#version ⇒ String
Returns the browser version.
Methods inherited from PlaywrightApi
Constructor Details
This class inherits a constructor from Playwright::PlaywrightApi
Instance Method Details
#bind(title, host: nil, port: nil, workspaceDir: nil) ⇒ Hash[untyped, untyped]
Binds the browser to a named pipe or web socket, making it available for other clients to connect to.
179 180 181 |
# File 'lib/playwright_api/browser.rb', line 179 def bind(title, host: nil, port: nil, workspaceDir: nil) wrap_impl(@impl.bind(unwrap_impl(title), host: unwrap_impl(host), port: unwrap_impl(port), workspaceDir: unwrap_impl(workspaceDir))) end |
#browser_type ⇒ BrowserType
Get the browser type (chromium, firefox or webkit) that the browser belongs to.
22 23 24 |
# File 'lib/playwright_api/browser.rb', line 22 def browser_type wrap_impl(@impl.browser_type) end |
#close(reason: nil) ⇒ void
This method returns an undefined value.
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.
NOTE: This is similar to force-quitting the browser. To close pages gracefully and ensure you receive page close events, call [method: BrowserContext.close] on any BrowserContext instances you explicitly created earlier using [method: Browser.newContext] before calling [method: Browser.close].
The Browser object itself is considered to be disposed and cannot be used anymore.
36 37 38 |
# File 'lib/playwright_api/browser.rb', line 36 def close(reason: nil) wrap_impl(@impl.close(reason: unwrap_impl(reason))) end |
#connected? ⇒ Boolean
Indicates that the browser is connected.
57 58 59 |
# File 'lib/playwright_api/browser.rb', line 57 def connected? wrap_impl(@impl.connected?) end |
#contexts ⇒ Array[untyped]
Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.
Usage
browser = pw.webkit.launch()
print(len(browser.contexts)) # prints `0`
context = browser.new_context()
print(len(browser.contexts)) # prints `1`
51 52 53 |
# File 'lib/playwright_api/browser.rb', line 51 def contexts wrap_impl(@impl.contexts) end |
#new_browser_cdp_session ⇒ CDPSession
NOTE: CDP Sessions are only supported on Chromium-based browsers.
Returns the newly created browser session.
65 66 67 |
# File 'lib/playwright_api/browser.rb', line 65 def new_browser_cdp_session wrap_impl(@impl.new_browser_cdp_session) end |
#new_context(acceptDownloads: nil, baseURL: nil, bypassCSP: nil, clientCertificates: nil, colorScheme: nil, contrast: nil, deviceScaleFactor: nil, extraHTTPHeaders: nil, forcedColors: 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_content: nil, record_har_mode: nil, record_har_omit_content: nil, record_har_path: nil, record_har_url_filter: nil, record_video_dir: nil, record_video_fps: nil, record_video_size: nil, reducedMotion: nil, screen: nil, serviceWorkers: nil, storageState: nil, strictSelectors: nil, timezoneId: nil, userAgent: nil, viewport: nil) {|arg0| ... } ⇒ BrowserContext
Creates a new browser context. It won't share cookies/cache with other browser contexts.
NOTE: If directly using this method to create BrowserContexts, it is best practice to explicitly close the returned context via [method: BrowserContext.close] when your code is done with the BrowserContext,
and before calling [method: Browser.close]. This will ensure the context is closed gracefully and any artifacts—like HARs and videos—are fully flushed and saved.
Usage
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("https://example.com")
# gracefully close up everything
context.close()
browser.close()
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/playwright_api/browser.rb', line 89 def new_context( acceptDownloads: nil, baseURL: nil, bypassCSP: nil, clientCertificates: nil, colorScheme: nil, contrast: nil, deviceScaleFactor: nil, extraHTTPHeaders: nil, forcedColors: 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_content: nil, record_har_mode: nil, record_har_omit_content: nil, record_har_path: nil, record_har_url_filter: nil, record_video_dir: nil, record_video_fps: nil, record_video_size: nil, reducedMotion: nil, screen: nil, serviceWorkers: nil, storageState: nil, strictSelectors: 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), clientCertificates: unwrap_impl(clientCertificates), colorScheme: unwrap_impl(colorScheme), contrast: unwrap_impl(contrast), deviceScaleFactor: unwrap_impl(deviceScaleFactor), extraHTTPHeaders: unwrap_impl(extraHTTPHeaders), forcedColors: unwrap_impl(forcedColors), 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(), proxy: unwrap_impl(proxy), record_har_content: unwrap_impl(record_har_content), record_har_mode: unwrap_impl(record_har_mode), record_har_omit_content: unwrap_impl(record_har_omit_content), record_har_path: unwrap_impl(record_har_path), record_har_url_filter: unwrap_impl(record_har_url_filter), record_video_dir: unwrap_impl(record_video_dir), record_video_fps: unwrap_impl(record_video_fps), record_video_size: unwrap_impl(record_video_size), reducedMotion: unwrap_impl(reducedMotion), screen: unwrap_impl(screen), serviceWorkers: unwrap_impl(serviceWorkers), storageState: unwrap_impl(storageState), strictSelectors: unwrap_impl(strictSelectors), timezoneId: unwrap_impl(timezoneId), userAgent: unwrap_impl(userAgent), viewport: unwrap_impl(), &wrap_block_call(block))) end |
#new_page(acceptDownloads: nil, baseURL: nil, bypassCSP: nil, clientCertificates: nil, colorScheme: nil, contrast: nil, deviceScaleFactor: nil, extraHTTPHeaders: nil, forcedColors: 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_content: nil, record_har_mode: nil, record_har_omit_content: nil, record_har_path: nil, record_har_url_filter: nil, record_video_dir: nil, record_video_fps: nil, record_video_size: nil, reducedMotion: nil, screen: nil, serviceWorkers: nil, storageState: nil, strictSelectors: nil, timezoneId: nil, userAgent: nil, viewport: nil) {|arg0| ... } ⇒ Page
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.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/playwright_api/browser.rb', line 136 def new_page( acceptDownloads: nil, baseURL: nil, bypassCSP: nil, clientCertificates: nil, colorScheme: nil, contrast: nil, deviceScaleFactor: nil, extraHTTPHeaders: nil, forcedColors: 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_content: nil, record_har_mode: nil, record_har_omit_content: nil, record_har_path: nil, record_har_url_filter: nil, record_video_dir: nil, record_video_fps: nil, record_video_size: nil, reducedMotion: nil, screen: nil, serviceWorkers: nil, storageState: nil, strictSelectors: 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), clientCertificates: unwrap_impl(clientCertificates), colorScheme: unwrap_impl(colorScheme), contrast: unwrap_impl(contrast), deviceScaleFactor: unwrap_impl(deviceScaleFactor), extraHTTPHeaders: unwrap_impl(extraHTTPHeaders), forcedColors: unwrap_impl(forcedColors), 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(), proxy: unwrap_impl(proxy), record_har_content: unwrap_impl(record_har_content), record_har_mode: unwrap_impl(record_har_mode), record_har_omit_content: unwrap_impl(record_har_omit_content), record_har_path: unwrap_impl(record_har_path), record_har_url_filter: unwrap_impl(record_har_url_filter), record_video_dir: unwrap_impl(record_video_dir), record_video_fps: unwrap_impl(record_video_fps), record_video_size: unwrap_impl(record_video_size), reducedMotion: unwrap_impl(reducedMotion), screen: unwrap_impl(screen), serviceWorkers: unwrap_impl(serviceWorkers), storageState: unwrap_impl(storageState), strictSelectors: unwrap_impl(strictSelectors), timezoneId: unwrap_impl(timezoneId), userAgent: unwrap_impl(userAgent), viewport: unwrap_impl(), &wrap_block_call(block))) end |
#off(event, callback) ⇒ Object
-- inherited from EventEmitter --
222 223 224 |
# File 'lib/playwright_api/browser.rb', line 222 def off(event, callback) event_emitter_proxy.off(event, callback) end |
#on(event, callback) ⇒ Object
-- inherited from EventEmitter --
234 235 236 |
# File 'lib/playwright_api/browser.rb', line 234 def on(event, callback) event_emitter_proxy.on(event, callback) end |
#once(event, callback) ⇒ Object
-- inherited from EventEmitter --
228 229 230 |
# File 'lib/playwright_api/browser.rb', line 228 def once(event, callback) event_emitter_proxy.once(event, callback) end |
#start_tracing(categories: nil, page: nil, path: nil, screenshots: nil) ⇒ void
This method returns an undefined value.
NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool. API to control Playwright Tracing could be found here.
You can use [method: Browser.startTracing] and [method: Browser.stopTracing] to create a trace file that can
be opened in Chrome DevTools performance panel.
Usage
browser.start_tracing(page, path="trace.json")
page.goto("https://www.google.com")
browser.stop_tracing()
196 197 198 |
# File 'lib/playwright_api/browser.rb', line 196 def start_tracing(categories: nil, page: nil, path: nil, screenshots: nil) wrap_impl(@impl.start_tracing(categories: unwrap_impl(categories), page: unwrap_impl(page), path: unwrap_impl(path), screenshots: unwrap_impl(screenshots))) end |
#stop_tracing ⇒ String
NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool. API to control Playwright Tracing could be found here.
Returns the buffer with trace data.
204 205 206 |
# File 'lib/playwright_api/browser.rb', line 204 def stop_tracing wrap_impl(@impl.stop_tracing) end |
#unbind ⇒ void
This method returns an undefined value.
Unbinds the browser server previously bound with [method: Browser.bind].
210 211 212 |
# File 'lib/playwright_api/browser.rb', line 210 def unbind wrap_impl(@impl.unbind) end |
#version ⇒ String
Returns the browser version.
216 217 218 |
# File 'lib/playwright_api/browser.rb', line 216 def version wrap_impl(@impl.version) end |