Method: Playwright::BrowserContext#expose_binding

Defined in:
lib/playwright_api/browser_context.rb

#expose_binding(name, callback, handle: nil) ⇒ Object

The method adds a function called ‘name` on the `window` object of every frame in every page in the context. When called, the function executes `callback` and returns a [Promise] which resolves to the return value of `callback`. If the `callback` returns a [Promise], it will be awaited.

The first argument of the ‘callback` function contains information about the caller: `{ browserContext: BrowserContext, page: Page, frame: Frame }`.

See [‘method: Page.exposeBinding`] for page-only version.

Usage

An example of exposing page URL to all frames in all pages in the context:

“‘python sync from playwright.sync_api import sync_playwright, Playwright

def run(playwright: Playwright):

webkit = playwright.webkit
browser = webkit.launch(headless=False)
context = browser.new_context()
context.expose_binding("pageURL", lambda source: source["page"].url)
page = context.new_page()
page.set_content("""
<script>
  async function onClick() {
    document.querySelector('div').textContent = await window.pageURL();
  }
</script>
<button onclick="onClick()">Click me</button>
<div></div>
""")
page.get_by_role("button").click()

with sync_playwright() as playwright:

run(playwright)

“‘



171
172
173
# File 'lib/playwright_api/browser_context.rb', line 171

def expose_binding(name, callback, handle: nil)
  wrap_impl(@impl.expose_binding(unwrap_impl(name), unwrap_impl(callback), handle: unwrap_impl(handle)))
end