CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-webdriverio

Next-gen browser and mobile automation test framework for Node.js

Pending
Overview
Eval results
Files

dialog-handling.mddocs/

Dialog Handling

Browser dialog and alert handling capabilities for managing JavaScript alerts, confirmations, and prompts during test automation.

Capabilities

Alert Management

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();

Dialog State Detection

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();

Dialog Types

WebdriverIO handles three main types of JavaScript dialogs:

Alert Dialogs

Simple notification dialogs with only an "OK" button.

// JavaScript: alert('Hello World!');
await browser.getAlertText(); // Returns: 'Hello World!'
await browser.acceptAlert();  // Clicks OK

Confirmation Dialogs

Dialogs 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)

Prompt Dialogs

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)

Error Handling

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');

Advanced Dialog Scenarios

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;
  }
}

Browser Compatibility

Dialog handling support varies by browser:

  • Chrome/Chromium: Full support for all dialog types
  • Firefox: Full support for all dialog types
  • Safari: Full support for all dialog types
  • Edge: Full support for all dialog types
  • Mobile browsers: Limited support, varies by platform

Notes

  • JavaScript dialogs are modal and block all interaction until handled
  • Only one dialog can be active at a time in a browser context
  • WebdriverIO automatically waits for dialogs to appear when commands are executed
  • Dialog text and default values depend on the browser's language settings
  • Some modern web applications use custom modal dialogs instead of native JavaScript dialogs

Install with Tessl CLI

npx tessl i tessl/npm-webdriverio

docs

browser-control.md

dialog-handling.md

element-interaction.md

element-selection.md

index.md

mobile-automation.md

session-management.md

testing-utilities.md

tile.json