Class: Playwright::Dialog

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

Overview

‘Dialog` objects are dispatched by page via the [`event: Page.dialog`] event.

An example of using ‘Dialog` class:

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

(async () => {

const browser = await chromium.launch();
const page = await browser.newPage();
page.on('dialog', async dialog => {
  console.log(dialog.message());
  await dialog.dismiss();
});
await page.evaluate(() => alert('1'));
await browser.close();

})(); “‘

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

public class Example {

public static void main(String[] args) {
  try (Playwright playwright = Playwright.create()) {
    BrowserType chromium = playwright.chromium();
    Browser browser = chromium.launch();
    Page page = browser.newPage();
    page.onDialog(dialog -> {
      System.out.println(dialog.message());
      dialog.dismiss();
    });
    page.evaluate("alert('1')");
    browser.close();
  }
}

} “‘

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

async def handle_dialog(dialog):

print(dialog.message)
await dialog.dismiss()

async def run(playwright):

chromium = playwright.chromium
browser = await chromium.launch()
page = await browser.new_page()
page.on("dialog", handle_dialog)
page.evaluate("alert('1')")
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 handle_dialog(dialog):

print(dialog.message)
dialog.dismiss()

def run(playwright):

chromium = playwright.chromium
browser = chromium.launch()
page = browser.new_page()
page.on("dialog", handle_dialog)
page.evaluate("alert('1')")
browser.close()

with sync_playwright() as playwright:

run(playwright)

“‘

> NOTE: Dialogs are dismissed automatically, unless there is a [‘event: Page.dialog`] listener. When listener is present, it must either [`method: Dialog.accept`] or [`method: Dialog.dismiss`] the dialog - otherwise the page will [freeze](developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#never_blocking) waiting for the dialog, and actions like click will never finish.

Instance Method Summary collapse

Methods inherited from PlaywrightApi

#==, #initialize, wrap

Constructor Details

This class inherits a constructor from Playwright::PlaywrightApi

Instance Method Details

#accept(promptText: nil) ⇒ Object

Returns when the dialog has been accepted.



90
91
92
# File 'lib/playwright_api/dialog.rb', line 90

def accept(promptText: nil)
  wrap_impl(@impl.accept(promptText: unwrap_impl(promptText)))
end

#accept_async(promptText: nil) ⇒ Object



115
116
117
# File 'lib/playwright_api/dialog.rb', line 115

def accept_async(promptText: nil)
  wrap_impl(@impl.accept_async(promptText: unwrap_impl(promptText)))
end

#default_valueObject

If dialog is prompt, returns default prompt value. Otherwise, returns empty string.



95
96
97
# File 'lib/playwright_api/dialog.rb', line 95

def default_value
  wrap_impl(@impl.default_value)
end

#dismissObject

Returns when the dialog has been dismissed.



100
101
102
# File 'lib/playwright_api/dialog.rb', line 100

def dismiss
  wrap_impl(@impl.dismiss)
end

#messageObject

A message displayed in the dialog.



105
106
107
# File 'lib/playwright_api/dialog.rb', line 105

def message
  wrap_impl(@impl.message)
end

#off(event, callback) ⇒ Object

– inherited from EventEmitter –



133
134
135
# File 'lib/playwright_api/dialog.rb', line 133

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

#on(event, callback) ⇒ Object

– inherited from EventEmitter –



127
128
129
# File 'lib/playwright_api/dialog.rb', line 127

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

#once(event, callback) ⇒ Object

– inherited from EventEmitter –



121
122
123
# File 'lib/playwright_api/dialog.rb', line 121

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

#typeObject

Returns dialog’s type, can be one of ‘alert`, `beforeunload`, `confirm` or `prompt`.



110
111
112
# File 'lib/playwright_api/dialog.rb', line 110

def type
  wrap_impl(@impl.type)
end