Skip to main content

getAlertText

Get the message of the user prompt (alert, confirm or prompt) that is open in this browsing context. Rejects with no such alert when none is open.

WebdriverIO dismisses prompts automatically unless something listens to the dialog event, so register a listener first.

Usage​

const context = await browser.url('https://webdriver.io')
await context.getAlertText()

Example​

getAlertText.js
it('reads the message of a prompt', async () => {
browser.on('dialog', () => {}) // keep prompts open
const page = await browser.url('https://webdriver.io')

const opened = new Promise((resolve) => browser.once('dialog', resolve))
await page.execute(() => setTimeout(() => window.alert('Saved')))
await opened
console.log(await page.getAlertText()) // outputs: "Saved"
await page.acceptAlert()
})

Returns​

  • <string> return: the prompt message

Welcome! How can I help?

WebdriverIO AI Copilot