|
| 1 | +const { test, expect } = require('@playwright/test'); |
| 2 | + |
| 3 | +test('Handle Alert and Confirm dialogs', async ({ page }) => { |
| 4 | + // Define selectors |
| 5 | + const targetURL = 'https://qa-practice.netlify.app/alerts'; |
| 6 | + const alertBtn = '#alert-btn'; |
| 7 | + const confirmBtn = '#confirm-btn'; |
| 8 | + |
| 9 | + // Navigate to the target URL |
| 10 | + await page.goto(targetURL); |
| 11 | + |
| 12 | + // Handle dialog events |
| 13 | + page.on('dialog', async (dialog) => { |
| 14 | + const message = dialog.message(); |
| 15 | + console.log(`Dialog message: ${message}`); |
| 16 | + |
| 17 | + if (message.includes('alert')) { |
| 18 | + await dialog.accept(); |
| 19 | + } else if (message.includes('OK')) { |
| 20 | + if (message.includes('Cancel')) { |
| 21 | + await dialog.dismiss(); |
| 22 | + } else { |
| 23 | + await dialog.accept(); |
| 24 | + } |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + // Trigger the alert dialog |
| 29 | + await page.locator(alertBtn).click(); |
| 30 | + |
| 31 | + // Wait to simulate interaction |
| 32 | + await page.waitForTimeout(1000); |
| 33 | + |
| 34 | + // Ensure the confirm button is ready before clicking |
| 35 | + await page.locator(confirmBtn).waitFor({ state: 'visible' }); |
| 36 | + await page.locator(confirmBtn).waitFor({ state: 'attached' }); |
| 37 | + await page.locator(confirmBtn).click(); |
| 38 | + |
| 39 | + // Wait to simulate interaction |
| 40 | + await page.waitForTimeout(1000); |
| 41 | + |
| 42 | + // Trigger the confirm dialog again |
| 43 | + await page.locator(confirmBtn).click(); |
| 44 | +}); |
0 commit comments