Class: Playwright::Browser

Inherits:
PlaywrightApi show all
Defined in:
lib/playwright_api/browser.rb

Overview

  • extends: [EventEmitter]

A Browser is created via [‘method: BrowserType.launch`]. An example of using a `Browser` to create a `Page`:

“‘js const { firefox } = require(’playwright’); // Or ‘chromium’ or ‘webkit’.

(async () =>

const browser = await firefox.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await browser.close();

)(); “‘

“‘java import com.microsoft.playwright.*;

public class Example {

public static void main(String[] args) {
  try (Playwright playwright = Playwright.create()) {
    BrowserType firefox = playwright.firefox()
    Browser browser = firefox.launch();
    Page page = browser.newPage();
    page.navigate('https://example.com');
    browser.close();
  }
}

} “‘

“‘python async import asyncio from playwright.async_api import async_playwright

async def run(playwright):

firefox = playwright.firefox
browser = await firefox.launch()
page = await browser.new_page()
await page.goto("https://example.com")
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):

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

Methods inherited from PlaywrightApi

#==, #initialize, wrap

Constructor Details

This class inherits a constructor from Playwright::PlaywrightApi

Instance Method Details

#closeObject

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.



73
74
75
# File 'lib/playwright_api/browser.rb', line 73

def close
  wrap_impl(@impl.close)
end

#connected?Boolean

Indicates that the browser is connected.

Returns:

  • (Boolean)


113
114
115
# File 'lib/playwright_api/browser.rb', line 113

def connected?
  wrap_impl(@impl.connected?)
end

#contextsObject

Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.

“‘js const browser = await pw.webkit.launch(); console.log(browser.contexts().length); // prints `0`

const context = await browser.newContext(); console.log(browser.contexts().length); // prints ‘1` “`

“‘java Browser browser = pw.webkit().launch(); System.out.println(browser.contexts().size()); // prints “0” BrowserContext context = browser.newContext(); System.out.println(browser.contexts().size()); // prints “1” “`

“‘python async browser = await pw.webkit.launch() print(len(browser.contexts())) # prints `0` context = await browser.new_context() print(len(browser.contexts())) # prints `1` “`

“‘python sync browser = pw.webkit.launch() print(len(browser.contexts())) # prints `0` context = browser.new_context() print(len(browser.contexts())) # prints `1` “`



108
109
110
# File 'lib/playwright_api/browser.rb', line 108

def contexts
  wrap_impl(@impl.contexts)
end

#new_browser_cdp_sessionObject

> NOTE: CDP Sessions are only supported on Chromium-based browsers.

Returns the newly created browser session.

Raises:

  • (NotImplementedError)


120
121
122
# File 'lib/playwright_api/browser.rb', line 120

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

#new_context(acceptDownloads: 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, screen: nil, storageState: nil, timezoneId: nil, userAgent: nil, viewport: nil, &block) ⇒ Object

Creates a new browser context. It won’t share cookies/cache with other browser contexts.

“‘js (async () =>

const browser = await playwright.firefox.launch();  // Or 'chromium' or 'webkit'.
// Create a new incognito browser context.
const context = await browser.newContext();
// Create a new page in a pristine context.
const page = await context.newPage();
await page.goto('https://example.com');

)(); “‘

“‘java Browser browser = playwright.firefox().launch(); // Or ’chromium’ or ‘webkit’. // Create a new incognito browser context. BrowserContext context = browser.newContext(); // Create a new page in a pristine context. Page page = context.newPage(); page.navigate(‘example.com’); “‘

“‘python async browser = await playwright.firefox.launch() # or “chromium” or “webkit”. # create a new incognito browser context. context = await browser.new_context() # create a new page in a pristine context. page = await context.new_page() await page.goto(“example.com”) “`

“‘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”) “`



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/playwright_api/browser.rb', line 164

def new_context(
      acceptDownloads: 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,
      screen: nil,
      storageState: nil,
      timezoneId: nil,
      userAgent: nil,
      viewport: nil,
      &block)
  wrap_impl(@impl.new_context(acceptDownloads: unwrap_impl(acceptDownloads), 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), 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

#new_page(acceptDownloads: 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, screen: nil, storageState: nil, timezoneId: nil, userAgent: nil, viewport: nil) ⇒ Object

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.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/playwright_api/browser.rb', line 199

def new_page(
      acceptDownloads: 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,
      screen: nil,
      storageState: nil,
      timezoneId: nil,
      userAgent: nil,
      viewport: nil)
  wrap_impl(@impl.new_page(acceptDownloads: unwrap_impl(acceptDownloads), 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), screen: unwrap_impl(screen), storageState: unwrap_impl(storageState), timezoneId: unwrap_impl(timezoneId), userAgent: unwrap_impl(userAgent), viewport: unwrap_impl(viewport)))
end

#off(event, callback) ⇒ Object

– inherited from EventEmitter –



281
282
283
# File 'lib/playwright_api/browser.rb', line 281

def off(event, callback)
  event_emitter_proxy.off(event, callback)
end

#on(event, callback) ⇒ Object

– inherited from EventEmitter –



275
276
277
# File 'lib/playwright_api/browser.rb', line 275

def on(event, callback)
  event_emitter_proxy.on(event, callback)
end

#once(event, callback) ⇒ Object

– inherited from EventEmitter –



269
270
271
# File 'lib/playwright_api/browser.rb', line 269

def once(event, callback)
  event_emitter_proxy.once(event, callback)
end

#start_tracing(page: nil, categories: nil, path: nil, screenshots: nil) ⇒ Object

> NOTE: Tracing is only supported on Chromium-based browsers.

You can use [‘method: Browser.startTracing`] and [`method: Browser.stopTracing`] to create a trace file that can be opened in Chrome DevTools performance panel.

“‘js await browser.startTracing(page, ’trace.json’); await page.goto(‘www.google.com’); await browser.stopTracing(); “‘

“‘python async await browser.start_tracing(page, path=“trace.json”) await page.goto(“www.google.com”) await browser.stop_tracing() “`

“‘python sync browser.start_tracing(page, path=“trace.json”) page.goto(“www.google.com”) browser.stop_tracing() “`



251
252
253
# File 'lib/playwright_api/browser.rb', line 251

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

#stop_tracingObject

> NOTE: Tracing is only supported on Chromium-based browsers.

Returns the buffer with trace data.



258
259
260
# File 'lib/playwright_api/browser.rb', line 258

def stop_tracing
  wrap_impl(@impl.stop_tracing)
end

#versionObject

Returns the browser version.



263
264
265
# File 'lib/playwright_api/browser.rb', line 263

def version
  wrap_impl(@impl.version)
end