Next-gen browser and mobile automation test framework for Node.js
—
Browser dialog and alert handling capabilities for managing JavaScript alerts, confirmations, and prompts during test automation.
Methods for handling JavaScript alert dialogs that appear during test execution.
/**
* Accept the currently displayed alert/dialog
* @returns Promise that resolves when alert is accepted
*/
acceptAlert(): Promise<void>;
/**
* Dismiss/cancel the currently displayed alert/dialog
* @returns Promise that resolves when alert is dismissed
*/
dismissAlert(): Promise<void>;
/**
* Get the text message from the currently displayed alert/dialog
* @returns Promise resolving to the alert message text
*/
getAlertText(): Promise<string>;
/**
* Send text to a prompt dialog
* @param text - Text to enter in the prompt dialog
* @returns Promise that resolves when text is sent
*/
sendAlertText(text: string): Promise<void>;Usage Examples:
import { remote } from "webdriverio";
const browser = await remote({ capabilities: { browserName: 'chrome' } });
await browser.url('https://example.com');
// Trigger an alert and handle it
await browser.$('#alert-button').click();
// Get alert text
const alertMessage = await browser.getAlertText();
console.log('Alert says:', alertMessage);
// Accept the alert
await browser.acceptAlert();
// Handle a confirmation dialog
await browser.$('#confirm-button').click();
const confirmText = await browser.getAlertText();
if (confirmText.includes('Are you sure?')) {
await browser.acceptAlert(); // Click OK
} else {
await browser.dismissAlert(); // Click Cancel
}
// Handle a prompt dialog
await browser.$('#prompt-button').click();
await browser.sendAlertText('My input text');
await browser.acceptAlert();Methods for detecting and waiting for dialog states.
/**
* Check if an alert dialog is currently displayed
* @returns Promise resolving to true if alert is present
*/
isAlertOpen(): Promise<boolean>;
/**
* Wait for an alert dialog to appear
* @param options - Wait options including timeout
* @returns Promise resolving when alert appears
*/
waitForAlert(options?: {
timeout?: number;
timeoutMsg?: string;
interval?: number;
}): Promise<void>;Usage Examples:
// Check if alert is present before interacting
const isPresent = await browser.isAlertOpen();
if (isPresent) {
const message = await browser.getAlertText();
await browser.acceptAlert();
}
// Wait for alert to appear after action
await browser.$('#async-alert-button').click();
await browser.waitForAlert({ timeout: 5000 });
await browser.acceptAlert();WebdriverIO handles three main types of JavaScript dialogs:
Simple notification dialogs with only an "OK" button.
// JavaScript: alert('Hello World!');
await browser.getAlertText(); // Returns: 'Hello World!'
await browser.acceptAlert(); // Clicks OKDialogs with "OK" and "Cancel" buttons for user confirmation.
// JavaScript: confirm('Are you sure?');
await browser.getAlertText(); // Returns: 'Are you sure?'
await browser.acceptAlert(); // Clicks OK (returns true to JavaScript)
// OR
await browser.dismissAlert(); // Clicks Cancel (returns false to JavaScript)Dialogs that request text input from the user.
// JavaScript: prompt('Enter your name:', 'Default');
await browser.getAlertText(); // Returns: 'Enter your name:'
await browser.sendAlertText('John'); // Enter text
await browser.acceptAlert(); // Clicks OK (returns 'John' to JavaScript)Common patterns for robust dialog handling with error management.
/**
* Safely handle dialogs with try-catch pattern
*/
async function handleDialog(browser: WebdriverIO.Browser, expectedText?: string) {
try {
// Check if dialog is present
const isOpen = await browser.isAlertOpen();
if (!isOpen) {
return false;
}
// Get and validate dialog text
const alertText = await browser.getAlertText();
if (expectedText && !alertText.includes(expectedText)) {
console.warn(`Unexpected dialog text: ${alertText}`);
}
// Accept the dialog
await browser.acceptAlert();
return true;
} catch (error) {
console.error('Error handling dialog:', error);
return false;
}
}
// Usage
const handled = await handleDialog(browser, 'Success');Handling complex dialog scenarios in real-world applications.
// Handle multiple sequential dialogs
async function handleSequentialDialogs(browser: WebdriverIO.Browser) {
const dialogTexts: string[] = [];
while (await browser.isAlertOpen()) {
const text = await browser.getAlertText();
dialogTexts.push(text);
if (text.includes('error')) {
await browser.dismissAlert();
break;
} else {
await browser.acceptAlert();
}
// Small delay for next dialog to appear
await browser.pause(100);
}
return dialogTexts;
}
// Handle dialogs with dynamic timeouts
async function handleDialogWithTimeout(browser: WebdriverIO.Browser, maxWait: number = 3000) {
try {
await browser.waitForAlert({ timeout: maxWait });
const text = await browser.getAlertText();
await browser.acceptAlert();
return text;
} catch (error) {
console.log('No dialog appeared within timeout');
return null;
}
}Dialog handling support varies by browser:
Install with Tessl CLI
npx tessl i tessl/npm-webdriverio