krishworkstech.com

Handling the Dialogs

Handling the Dialogs

Introduction

The Playwright framework is capable of interacting with web page dialogs, including alert, confirm, and prompt, in addition to beforeunload confirmation. For print dialogs, see Print.

coverimage 2 - part 7 (1)

alert(), confirm(), prompt() dialogs

By default, Playwright automatically dismisses dialogs, eliminating the need for you to manage them. Nevertheless, you have the option to register a dialog handler prior to the action that initiates the dialog, allowing you to either dialog.accept() or dialog.dismiss() it.

 

page.on(‘dialog’, dialog => dialog.accept());

await page.getByRole(‘button’).click();



Note : 

page.on(‘dialog’) listener must handle the dialog. Otherwise your action will stall, be it locator.click() or something else. That’s because dialogs in Web are modals and therefore block further page execution until they are handled.

 

As a result, the following snippet will never resolve:

 

page.on(‘dialog’, dialog => console.log(dialog.message()));

await page.getByRole(‘button’).click(); // Will hang here

 

Note : 

If there is no listener for page.on(‘dialog’), all dialogs are automatically dismissed.

beforeunload dialog

When page.close() is called with the truthy runBeforeUnload parameter, the page executes its unload handlers. This is the sole instance in which page.close() does not pause for the page to fully close, as it is possible for the page to remain open at the conclusion of the operation.

 

You have the option to register a dialog handler to manage the beforeunload dialog on your own:

 

page.on(‘dialog’, async dialog => {

  assert(dialog.type() === ‘beforeunload’);

  await dialog.dismiss();

});

await page.close({ runBeforeUnload: true });

Print dialogs

To confirm that a print dialog was initiated through window.print, you may utilize the following code snippet:

 

await page.goto(‘<url>’);

 

await page.evaluate(‘(() => {window.waitForPrintDialog = new Promise(f => window.print = f);})()’);

await page.getByText(‘Print it!’).click();

 

await page.waitForFunction(‘window.waitForPrintDialog’);

 

This will pause until the print dialog is displayed following the button click. Ensure that you assess the script prior to clicking the button or after the page has fully loaded.

Playwright Essentials: A Step-by-Step Journey into Test Automation

cover - part 9

Part 01: Master Playwright Testing – Framework from Scratch Guide

cover - part 10

Part 02: How to install Playwright

cover - part 11

Part 03: Basic Playwright Test Structure

cover - part 13 (1)

Part 04: Importance of Playwright configuration file and its details to run the tests

cover - part 17

Part 05: Writing Tests in PlayWright

cover - part 19

Part 06: Playwright Actions Illustrated

cover - part 13 (1)

Part 07: Auto-waiting

cover - part 17

Part 08: Handling the Dialogs

Scroll to Top
  • Schematic design
  • PCB and schematic source files
  • Assembling drawing files
  • Providing prototype/sample and production PCB service
  • Testing and validation of designed hardware
  • HIPAA
  • Azure Key
  • Management
  • ES, Checksum,
  • MD5sum
  • AWS
  • Azure
  • GCP
  • DigitalOcean
  • Kotlin
  • Python
  • Tensorflow
  • Computer Vision
  • ECG
  • SPO2
  • Heart Rate
  • Glucometer
  • Blood Pressure
  • UX UI Process
  • Figma and FigJam
  • Adobe Suite
  • Selenium Java
  • Postman
  • Swagger
  • Jmeter
  • SQL
  • Java Scripter
  • Test ng
  • Extents Reports
  • Flutter
  • Java
  • Kotlin
  • Swift
  • Dart
  • React JS
  • Python
  • NodeJS
  • Django
  • HTML, CSS, JS
RDBMS
  • PostgreSQL
  • Oracle
  • MySQL
  • MariaDB
No SQL Based
  • MongoDB
  • GCP
  • FirestoreDB
  • DynamoDB
  • Azure
  • CosmosDB
  • AWS