Class: Playwright::BrowserType

Inherits:
PlaywrightApi show all
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();

)(); “‘

Instance Method Summary collapse

Methods inherited from PlaywrightApi

from_channel_owner

Instance Method Details

#connect(params) ⇒ Object

This methods attaches Playwright to an existing browser instance.

Raises:

  • (NotImplementedError)


20
21
22
# File 'lib/playwright_api/browser_type.rb', line 20

def connect(params)
  raise NotImplementedError.new('connect is not implemented yet.')
end

#executable_pathObject

A path where Playwright expects to find a bundled browser executable.



25
26
27
# File 'lib/playwright_api/browser_type.rb', line 25

def executable_path
  wrap_channel_owner(@channel_owner.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']

}); “‘

> 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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/playwright_api/browser_type.rb', line 54

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_channel_owner(@channel_owner.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.

Raises:

  • (NotImplementedError)


79
80
81
82
83
84
85
86
87
88
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
# File 'lib/playwright_api/browser_type.rb', line 79

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();

})(); “‘

Raises:

  • (NotImplementedError)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/playwright_api/browser_type.rb', line 138

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

#nameObject

Returns browser name. For example: ‘’chromium’‘, `’webkit’‘ or `’firefox’‘.



159
160
161
# File 'lib/playwright_api/browser_type.rb', line 159

def name
  wrap_channel_owner(@channel_owner.name)
end