Class: Playwright::BrowserType
- Inherits:
-
PlaywrightApi
- Object
- PlaywrightApi
- Playwright::BrowserType
- Defined in:
- lib/playwright_api/browser_type.rb,
sig/playwright.rbs
Overview
BrowserType provides methods to launch a specific browser instance or connect to an existing one. The following is a typical example of using Playwright to drive automation:
from playwright.sync_api import sync_playwright, Playwright
def run(playwright: Playwright):
chromium = playwright.chromium
browser = chromium.launch()
page = browser.new_page()
page.goto("https://example.com")
# other actions...
browser.close()
with sync_playwright() as playwright:
run(playwright)
Instance Method Summary collapse
-
#connect(endpoint, exposeNetwork: nil, headers: nil, slowMo: nil, timeout: nil, &block) ⇒ void
This method attaches Playwright to an existing browser instance created via
BrowserType.launchServerin Node.js. -
#connect_over_cdp(endpointURL, artifactsDir: nil, headers: nil, isLocal: nil, noDefaults: nil, slowMo: nil, timeout: nil, &block) ⇒ void
This method attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.
-
#executable_path ⇒ String
A path where Playwright expects to find a bundled browser executable.
-
#launch(args: nil, artifactsDir: nil, channel: nil, chromiumSandbox: nil, downloadsPath: nil, env: nil, executablePath: nil, firefoxUserPrefs: nil, handleSIGHUP: nil, handleSIGINT: nil, handleSIGTERM: nil, headless: nil, ignoreDefaultArgs: nil, proxy: nil, slowMo: nil, timeout: nil, tracesDir: nil, &block) ⇒ void
Returns the browser instance.
-
#launch_persistent_context(userDataDir, acceptDownloads: nil, args: nil, artifactsDir: nil, baseURL: nil, bypassCSP: nil, channel: nil, chromiumSandbox: nil, clientCertificates: nil, colorScheme: nil, contrast: nil, deviceScaleFactor: nil, downloadsPath: nil, env: nil, executablePath: nil, extraHTTPHeaders: nil, firefoxUserPrefs: nil, forcedColors: nil, geolocation: nil, handleSIGHUP: nil, handleSIGINT: nil, handleSIGTERM: nil, hasTouch: nil, headless: nil, httpCredentials: nil, ignoreDefaultArgs: 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_size: nil, reducedMotion: nil, screen: nil, serviceWorkers: nil, slowMo: nil, strictSelectors: nil, timeout: nil, timezoneId: nil, tracesDir: nil, userAgent: nil, viewport: nil, &block) ⇒ void
Returns the persistent browser context instance.
-
#name ⇒ String
Returns browser name.
-
#off(event, callback) ⇒ Object
-- inherited from EventEmitter --.
-
#on(event, callback) ⇒ Object
-- inherited from EventEmitter --.
-
#once(event, callback) ⇒ Object
-- inherited from EventEmitter --.
Methods inherited from PlaywrightApi
Constructor Details
This class inherits a constructor from Playwright::PlaywrightApi
Instance Method Details
#connect(endpoint, exposeNetwork: nil, headers: nil, slowMo: nil, timeout: nil, &block) ⇒ void
This method returns an undefined value.
This method attaches Playwright to an existing browser instance created via BrowserType.launchServer in Node.js.
NOTE: The major and minor version of the Playwright instance that connects needs to match the version of Playwright that launches the browser (1.2.3 → is compatible with 1.2.x).
26 27 28 29 30 31 32 33 34 |
# File 'lib/playwright_api/browser_type.rb', line 26 def connect( endpoint, exposeNetwork: nil, headers: nil, slowMo: nil, timeout: nil, &block) wrap_impl(@impl.connect(unwrap_impl(endpoint), exposeNetwork: unwrap_impl(exposeNetwork), headers: unwrap_impl(headers), slowMo: unwrap_impl(slowMo), timeout: unwrap_impl(timeout), &wrap_block_call(block))) end |
#connect_over_cdp(endpointURL, artifactsDir: nil, headers: nil, isLocal: nil, noDefaults: nil, slowMo: nil, timeout: nil, &block) ⇒ void
This method returns an undefined value.
This method attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.
The default browser context is accessible via [method: Browser.contexts].
NOTE: Connecting over the Chrome DevTools Protocol is only supported for Chromium-based browsers.
NOTE: This connection is significantly lower fidelity than the Playwright protocol connection via [method: BrowserType.connect]. If you are experiencing issues or attempting to use advanced functionality, you probably want to use [method: BrowserType.connect].
Usage
browser = playwright.chromium.connect_over_cdp("http://localhost:9222")
default_context = browser.contexts[0]
page = default_context.pages[0]
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/playwright_api/browser_type.rb', line 52 def connect_over_cdp( endpointURL, artifactsDir: nil, headers: nil, isLocal: nil, noDefaults: nil, slowMo: nil, timeout: nil, &block) wrap_impl(@impl.connect_over_cdp(unwrap_impl(endpointURL), artifactsDir: unwrap_impl(artifactsDir), headers: unwrap_impl(headers), isLocal: unwrap_impl(isLocal), noDefaults: unwrap_impl(noDefaults), slowMo: unwrap_impl(slowMo), timeout: unwrap_impl(timeout), &wrap_block_call(block))) end |
#executable_path ⇒ String
A path where Playwright expects to find a bundled browser executable.
66 67 68 |
# File 'lib/playwright_api/browser_type.rb', line 66 def executable_path wrap_impl(@impl.executable_path) end |
#launch(args: nil, artifactsDir: nil, channel: nil, chromiumSandbox: nil, downloadsPath: nil, env: nil, executablePath: nil, firefoxUserPrefs: nil, handleSIGHUP: nil, handleSIGINT: nil, handleSIGTERM: nil, headless: nil, ignoreDefaultArgs: nil, proxy: nil, slowMo: nil, timeout: nil, tracesDir: nil, &block) ⇒ void
This method returns an undefined value.
Returns the browser instance.
Usage
You can use ignoreDefaultArgs to filter out --mute-audio from default arguments:
browser = playwright.chromium.launch( # or "firefox" or "webkit".
ignore_default_args=["--mute-audio"]
)
Chromium-only Playwright can also be used to control the Google Chrome or Microsoft Edge browsers, but it works best with the version of Chromium it is bundled with. There is no guarantee it will work with any other version. Use
executablePathoption with extreme caution.
If Google Chrome (rather than Chromium) is preferred, a Chrome Canary or Dev Channel build is suggested.
Stock browsers like Google Chrome and Microsoft Edge are suitable for tests that require proprietary media codecs for video playback. See this article for other differences between Chromium and Chrome. This article describes some differences for Linux users.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/playwright_api/browser_type.rb', line 98 def launch( args: nil, artifactsDir: nil, channel: nil, chromiumSandbox: nil, downloadsPath: nil, env: nil, executablePath: nil, firefoxUserPrefs: nil, handleSIGHUP: nil, handleSIGINT: nil, handleSIGTERM: nil, headless: nil, ignoreDefaultArgs: nil, proxy: nil, slowMo: nil, timeout: nil, tracesDir: nil, &block) wrap_impl(@impl.launch(args: unwrap_impl(args), artifactsDir: unwrap_impl(artifactsDir), channel: unwrap_impl(channel), chromiumSandbox: unwrap_impl(chromiumSandbox), downloadsPath: unwrap_impl(downloadsPath), env: unwrap_impl(env), executablePath: unwrap_impl(executablePath), firefoxUserPrefs: unwrap_impl(firefoxUserPrefs), handleSIGHUP: unwrap_impl(handleSIGHUP), handleSIGINT: unwrap_impl(handleSIGINT), handleSIGTERM: unwrap_impl(handleSIGTERM), headless: unwrap_impl(headless), ignoreDefaultArgs: unwrap_impl(ignoreDefaultArgs), proxy: unwrap_impl(proxy), slowMo: unwrap_impl(slowMo), timeout: unwrap_impl(timeout), tracesDir: unwrap_impl(tracesDir), &wrap_block_call(block))) end |
#launch_persistent_context(userDataDir, acceptDownloads: nil, args: nil, artifactsDir: nil, baseURL: nil, bypassCSP: nil, channel: nil, chromiumSandbox: nil, clientCertificates: nil, colorScheme: nil, contrast: nil, deviceScaleFactor: nil, downloadsPath: nil, env: nil, executablePath: nil, extraHTTPHeaders: nil, firefoxUserPrefs: nil, forcedColors: nil, geolocation: nil, handleSIGHUP: nil, handleSIGINT: nil, handleSIGTERM: nil, hasTouch: nil, headless: nil, httpCredentials: nil, ignoreDefaultArgs: 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_size: nil, reducedMotion: nil, screen: nil, serviceWorkers: nil, slowMo: nil, strictSelectors: nil, timeout: nil, timezoneId: nil, tracesDir: nil, userAgent: nil, viewport: nil, &block) ⇒ void
This method returns an undefined value.
Returns the persistent browser context instance.
Launches browser that uses persistent storage located at userDataDir and returns the only context. Closing
this context will automatically close the browser.
125 126 127 128 129 130 131 132 133 134 135 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 176 177 178 179 |
# File 'lib/playwright_api/browser_type.rb', line 125 def launch_persistent_context( userDataDir, acceptDownloads: nil, args: nil, artifactsDir: nil, baseURL: nil, bypassCSP: nil, channel: nil, chromiumSandbox: nil, clientCertificates: nil, colorScheme: nil, contrast: nil, deviceScaleFactor: nil, downloadsPath: nil, env: nil, executablePath: nil, extraHTTPHeaders: nil, firefoxUserPrefs: nil, forcedColors: nil, geolocation: nil, handleSIGHUP: nil, handleSIGINT: nil, handleSIGTERM: nil, hasTouch: nil, headless: nil, httpCredentials: nil, ignoreDefaultArgs: 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_size: nil, reducedMotion: nil, screen: nil, serviceWorkers: nil, slowMo: nil, strictSelectors: nil, timeout: nil, timezoneId: nil, tracesDir: nil, userAgent: nil, viewport: nil, &block) wrap_impl(@impl.launch_persistent_context(unwrap_impl(userDataDir), acceptDownloads: unwrap_impl(acceptDownloads), args: unwrap_impl(args), artifactsDir: unwrap_impl(artifactsDir), baseURL: unwrap_impl(baseURL), bypassCSP: unwrap_impl(bypassCSP), channel: unwrap_impl(channel), chromiumSandbox: unwrap_impl(chromiumSandbox), clientCertificates: unwrap_impl(clientCertificates), colorScheme: unwrap_impl(colorScheme), contrast: unwrap_impl(contrast), deviceScaleFactor: unwrap_impl(deviceScaleFactor), downloadsPath: unwrap_impl(downloadsPath), env: unwrap_impl(env), executablePath: unwrap_impl(executablePath), extraHTTPHeaders: unwrap_impl(extraHTTPHeaders), firefoxUserPrefs: unwrap_impl(firefoxUserPrefs), forcedColors: unwrap_impl(forcedColors), geolocation: unwrap_impl(geolocation), handleSIGHUP: unwrap_impl(handleSIGHUP), handleSIGINT: unwrap_impl(handleSIGINT), handleSIGTERM: unwrap_impl(handleSIGTERM), hasTouch: unwrap_impl(hasTouch), headless: unwrap_impl(headless), httpCredentials: unwrap_impl(httpCredentials), ignoreDefaultArgs: unwrap_impl(ignoreDefaultArgs), 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_size: unwrap_impl(record_video_size), reducedMotion: unwrap_impl(reducedMotion), screen: unwrap_impl(screen), serviceWorkers: unwrap_impl(serviceWorkers), slowMo: unwrap_impl(slowMo), strictSelectors: unwrap_impl(strictSelectors), timeout: unwrap_impl(timeout), timezoneId: unwrap_impl(timezoneId), tracesDir: unwrap_impl(tracesDir), userAgent: unwrap_impl(userAgent), viewport: unwrap_impl(), &wrap_block_call(block))) end |
#name ⇒ String
Returns browser name. For example: 'chromium', 'webkit' or 'firefox'.
183 184 185 |
# File 'lib/playwright_api/browser_type.rb', line 183 def name wrap_impl(@impl.name) end |
#off(event, callback) ⇒ Object
-- inherited from EventEmitter --
189 190 191 |
# File 'lib/playwright_api/browser_type.rb', line 189 def off(event, callback) event_emitter_proxy.off(event, callback) end |
#on(event, callback) ⇒ Object
-- inherited from EventEmitter --
201 202 203 |
# File 'lib/playwright_api/browser_type.rb', line 201 def on(event, callback) event_emitter_proxy.on(event, callback) end |
#once(event, callback) ⇒ Object
-- inherited from EventEmitter --
195 196 197 |
# File 'lib/playwright_api/browser_type.rb', line 195 def once(event, callback) event_emitter_proxy.once(event, callback) end |