class Playwright::BrowserContext
“‘
context.close()
# dispose context once it is no longer needed.
page.goto(“example.com”)
page = context.new_page()
# create a new page inside context.
context = browser.new_context()
# create a new incognito browser context
“`python sync
contexts don’t write any browsing data to disk.
Playwright allows creating “incognito” browser contexts with [‘method: Browser.newContext`] method. “Incognito” browser
context.
If a page opens another page, e.g. with a `window.open` call, the popup will belong to the parent page’s browser
BrowserContexts provide a way to operate multiple independent browser sessions.
- extends: [EventEmitter]
def add_cookies(cookies)
browser_context.add_cookies([cookie_object1, cookie_object2])
```python sync
**Usage**
obtained via [`method: BrowserContext.cookies`].
Adds cookies into this browser context. All pages within this context will have these cookies installed. Cookies can be
def add_cookies(cookies) wrap_impl(@impl.add_cookies(unwrap_impl(cookies))) end
def add_init_script(path: nil, script: nil)
**NOTE**: The order of evaluation of multiple scripts installed via [`method: BrowserContext.addInitScript`] and
```
browser_context.add_init_script(path="preload.js")
# in your playwright script, assuming the preload.js file is in same directory.
```python sync
An example of overriding `Math.random` before the page loads:
**Usage**
the JavaScript environment, e.g. to seed `Math.random`.
The script is evaluated after the document was created but before any of its scripts were run. This is useful to amend
- Whenever a child frame is attached or navigated in any page in the browser context. In this case, the script is evaluated in the context of the newly attached frame.
- Whenever a page is created in the browser context or is navigated.
Adds a script which would be evaluated in one of the following scenarios:
def add_init_script(path: nil, script: nil) wrap_impl(@impl.add_init_script(path: unwrap_impl(path), script: unwrap_impl(script))) end
def background_pages
**NOTE**: Background pages are only supported on Chromium-based browsers.
def background_pages wrap_impl(@impl.background_pages) end
def browser
def browser wrap_impl(@impl.browser) end
def browser=(req)
def browser=(req) wrap_impl(@impl.browser=(unwrap_impl(req))) end
def clear_cookies
def clear_cookies wrap_impl(@impl.clear_cookies) end
def clear_permissions
context.clear_permissions()
# do stuff ..
context.grant_permissions(["clipboard-read"])
context = browser.new_context()
```python sync
**Usage**
Clears all permission overrides for the browser context.
def clear_permissions wrap_impl(@impl.clear_permissions) end
def close(reason: nil)
Closes the browser context. All the pages that belong to the browser context will be closed.
def close(reason: nil) wrap_impl(@impl.close(reason: unwrap_impl(reason))) end
def cookies(urls: nil)
If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLs
def cookies(urls: nil) wrap_impl(@impl.cookies(urls: unwrap_impl(urls))) end
def enable_debug_console!
def enable_debug_console! wrap_impl(@impl.enable_debug_console!) end
def event_emitter_proxy
def event_emitter_proxy _emitter_proxy ||= EventEmitterProxy.new(self, @impl)
def expect_console_message(predicate: nil, timeout: nil, &block)
`ConsoleMessage` value into the `predicate` function and waits for `predicate(message)` to return a truthy value.
Performs action and waits for a `ConsoleMessage` to be logged by in the pages in the context. If predicate is provided, it passes
def expect_console_message(predicate: nil, timeout: nil, &block) wrap_impl(@impl.expect_console_message(predicate: unwrap_impl(predicate), timeout: unwrap_impl(timeout), &wrap_block_call(block))) end
def expect_event(event, predicate: nil, timeout: nil, &block)
page = event_info.value
page.get_by_role("button").click()
with context.expect_event("page") as event_info:
```python sync
**Usage**
value. Will throw an error if the context closes before the event is fired. Returns the event data value.
Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy
def expect_event(event, predicate: nil, timeout: nil, &block) wrap_impl(@impl.expect_event(unwrap_impl(event), predicate: unwrap_impl(predicate), timeout: unwrap_impl(timeout), &wrap_block_call(block))) end
def expect_page(predicate: nil, timeout: nil, &block)
`Page` value into the `predicate` function and waits for `predicate(event)` to return a truthy value.
Performs action and waits for a new `Page` to be created in the context. If predicate is provided, it passes
def expect_page(predicate: nil, timeout: nil, &block) wrap_impl(@impl.expect_page(predicate: unwrap_impl(predicate), timeout: unwrap_impl(timeout), &wrap_block_call(block))) end
def expose_binding(name, callback, handle: nil)
""")
Or click me
Click me
document.addEventListener('click', event => window.clicked(event.target));
}
document.querySelector('div').textContent = await window.pageURL();
async function onClick() {
}
document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT');
async function onClick() {