Class: Playwright::BrowserType
- Inherits:
-
PlaywrightApi
- Object
- PlaywrightApi
- Playwright::BrowserType
- Defined in:
- lib/playwright_api/browser_type.rb
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:
“‘js const { chromium } = require(’playwright’); // Or ‘firefox’ or ‘webkit’.
(async () =>
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
// other actions...
await browser.close();
)(); “‘
“‘python async import asyncio from playwright.async_api import async_playwright
async def run(playwright):
chromium = playwright.chromium
browser = await chromium.launch()
page = await browser.new_page()
await page.goto("https://example.com")
# other actions...
await browser.close()
async def main():
async with async_playwright() as playwright:
await run(playwright)
asyncio.run(main()) “‘
“‘python sync from playwright.sync_api import sync_playwright
def run(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(params) ⇒ Object
This methods attaches Playwright to an existing browser instance.
-
#executable_path ⇒ Object
A path where Playwright expects to find a bundled browser executable.
-
#launch(args: nil, chromiumSandbox: nil, devtools: nil, downloadsPath: nil, env: nil, executablePath: nil, firefoxUserPrefs: nil, handleSIGHUP: nil, handleSIGINT: nil, handleSIGTERM: nil, headless: nil, ignoreDefaultArgs: nil, logger: nil, proxy: nil, slowMo: nil, timeout: nil, &block) ⇒ Object
Returns the browser instance.
-
#launch_persistent_context(userDataDir, acceptDownloads: nil, args: nil, bypassCSP: nil, chromiumSandbox: nil, colorScheme: nil, deviceScaleFactor: nil, devtools: nil, downloadsPath: nil, env: nil, executablePath: nil, extraHTTPHeaders: 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, logger: nil, offline: nil, permissions: nil, proxy: nil, recordHar: nil, recordVideo: nil, slowMo: nil, timeout: nil, timezoneId: nil, userAgent: nil, videoSize: nil, videosPath: nil, viewport: nil) ⇒ Object
Returns the persistent browser context instance.
-
#launch_server(args: nil, chromiumSandbox: nil, devtools: nil, downloadsPath: nil, env: nil, executablePath: nil, firefoxUserPrefs: nil, handleSIGHUP: nil, handleSIGINT: nil, handleSIGTERM: nil, headless: nil, ignoreDefaultArgs: nil, logger: nil, port: nil, proxy: nil, timeout: nil) ⇒ Object
Returns the browser app instance.
-
#name ⇒ Object
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
#==, from_channel_owner, #initialize
Constructor Details
This class inherits a constructor from Playwright::PlaywrightApi
Instance Method Details
#connect(params) ⇒ Object
This methods attaches Playwright to an existing browser instance.
53 54 55 |
# File 'lib/playwright_api/browser_type.rb', line 53 def connect(params) raise NotImplementedError.new('connect is not implemented yet.') end |
#executable_path ⇒ Object
A path where Playwright expects to find a bundled browser executable.
58 59 60 |
# File 'lib/playwright_api/browser_type.rb', line 58 def executable_path wrap_impl(@impl.executable_path) end |
#launch(args: nil, chromiumSandbox: nil, devtools: nil, downloadsPath: nil, env: nil, executablePath: nil, firefoxUserPrefs: nil, handleSIGHUP: nil, handleSIGINT: nil, handleSIGTERM: nil, headless: nil, ignoreDefaultArgs: nil, logger: nil, proxy: nil, slowMo: nil, timeout: nil, &block) ⇒ Object
Returns the browser instance.
You can use ignoreDefaultArgs to filter out --mute-audio from default arguments:
“‘js const browser = await chromium.launch({ // Or ’firefox’ or ‘webkit’.
ignoreDefaultArgs: ['--mute-audio']
}); “‘
“‘python async browser = await playwright.chromium.launch( # or “firefox” or “webkit”.
ignore_default_args=["--mute-audio"]
) “‘
“‘python sync browser = playwright.chromium.launch( # or “firefox” or “webkit”.
ignore_default_args=["--mute-audio"]
) “‘
> Chromium-only Playwright can also be used to control the Chrome browser, 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 executablePath option with extreme caution. > > If Google Chrome (rather than Chromium) is preferred, a [Chrome Canary](www.google.com/chrome/browser/canary.html) or [Dev Channel](www.chromium.org/getting-involved/dev-channel) build is suggested. > > In [‘method: BrowserType.launch`] above, any mention of Chromium also applies to Chrome. > > See [`this article`](www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/) for a description of the differences between Chromium and Chrome. [`This article`](chromium.googlesource.com/chromium/src/+/lkgr/docs/chromium_browser_vs_google_chrome.md) describes some differences for Linux users.
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 99 def launch( args: nil, chromiumSandbox: nil, devtools: nil, downloadsPath: nil, env: nil, executablePath: nil, firefoxUserPrefs: nil, handleSIGHUP: nil, handleSIGINT: nil, handleSIGTERM: nil, headless: nil, ignoreDefaultArgs: nil, logger: nil, proxy: nil, slowMo: nil, timeout: nil, &block) wrap_impl(@impl.launch(args: args, chromiumSandbox: chromiumSandbox, devtools: devtools, downloadsPath: downloadsPath, env: env, executablePath: executablePath, firefoxUserPrefs: firefoxUserPrefs, handleSIGHUP: handleSIGHUP, handleSIGINT: handleSIGINT, handleSIGTERM: handleSIGTERM, headless: headless, ignoreDefaultArgs: ignoreDefaultArgs, logger: logger, proxy: proxy, slowMo: slowMo, timeout: timeout, &wrap_block_call(block))) end |
#launch_persistent_context(userDataDir, acceptDownloads: nil, args: nil, bypassCSP: nil, chromiumSandbox: nil, colorScheme: nil, deviceScaleFactor: nil, devtools: nil, downloadsPath: nil, env: nil, executablePath: nil, extraHTTPHeaders: 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, logger: nil, offline: nil, permissions: nil, proxy: nil, recordHar: nil, recordVideo: nil, slowMo: nil, timeout: nil, timezoneId: nil, userAgent: nil, videoSize: nil, videosPath: nil, viewport: nil) ⇒ Object
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.
124 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 |
# File 'lib/playwright_api/browser_type.rb', line 124 def launch_persistent_context( userDataDir, acceptDownloads: nil, args: nil, bypassCSP: nil, chromiumSandbox: nil, colorScheme: nil, deviceScaleFactor: nil, devtools: nil, downloadsPath: nil, env: nil, executablePath: nil, extraHTTPHeaders: 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, logger: nil, offline: nil, permissions: nil, proxy: nil, recordHar: nil, recordVideo: nil, slowMo: nil, timeout: nil, timezoneId: nil, userAgent: nil, videoSize: nil, videosPath: nil, viewport: nil) raise NotImplementedError.new('launch_persistent_context is not implemented yet.') end |
#launch_server(args: nil, chromiumSandbox: nil, devtools: nil, downloadsPath: nil, env: nil, executablePath: nil, firefoxUserPrefs: nil, handleSIGHUP: nil, handleSIGINT: nil, handleSIGTERM: nil, headless: nil, ignoreDefaultArgs: nil, logger: nil, port: nil, proxy: nil, timeout: nil) ⇒ Object
Returns the browser app instance.
Launches browser server that client can connect to. An example of launching a browser executable and connecting to it later:
“‘js const { chromium } = require(’playwright’); // Or ‘webkit’ or ‘firefox’.
(async () => {
const browserServer = await chromium.launchServer();
const wsEndpoint = browserServer.wsEndpoint();
// Use web socket endpoint later to establish a connection.
const browser = await chromium.connect({ wsEndpoint });
// Close browser instance.
await browserServer.close();
})(); “‘
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/playwright_api/browser_type.rb', line 183 def launch_server( args: nil, chromiumSandbox: nil, devtools: nil, downloadsPath: nil, env: nil, executablePath: nil, firefoxUserPrefs: nil, handleSIGHUP: nil, handleSIGINT: nil, handleSIGTERM: nil, headless: nil, ignoreDefaultArgs: nil, logger: nil, port: nil, proxy: nil, timeout: nil) raise NotImplementedError.new('launch_server is not implemented yet.') end |
#name ⇒ Object
Returns browser name. For example: ‘’chromium’‘, `’webkit’‘ or `’firefox’‘.
204 205 206 |
# File 'lib/playwright_api/browser_type.rb', line 204 def name wrap_impl(@impl.name) end |
#off(event, callback) ⇒ Object
– inherited from EventEmitter –
216 217 218 |
# File 'lib/playwright_api/browser_type.rb', line 216 def off(event, callback) wrap_impl(@impl.off(event, callback)) end |
#on(event, callback) ⇒ Object
– inherited from EventEmitter –
210 211 212 |
# File 'lib/playwright_api/browser_type.rb', line 210 def on(event, callback) wrap_impl(@impl.on(event, callback)) end |
#once(event, callback) ⇒ Object
– inherited from EventEmitter –
222 223 224 |
# File 'lib/playwright_api/browser_type.rb', line 222 def once(event, callback) wrap_impl(@impl.once(event, callback)) end |