Handling Alerts in Playwright



Handling Alerts in Playwright

import {Page,expect,test,Locator,Browser,chromium,firefox,webkit} from ‘@playwright/test’;

//Handling Alert

test(‘Handle Alert’, async ({ page }) => {
    page.on(‘dialog’, async dialog => {
    console.log(dialog.type());      // alert
    console.log(dialog.message());   // Alert text
    await dialog.accept();
  });
  await page.goto(‘https://the-internet.herokuapp.com/javascript_alerts’);
  await page.locator(‘button:text(“Click for JS Alert”)’).click();
  await page.waitForTimeout(3000);
});
//Accept confirmation
test(‘Accept Confirmation’, async ({ page }) => {
  page.on(‘dialog’, async dialog => {
    console.log(dialog.type());
    console.log(dialog.message());
    await dialog.accept();
  });
  await page.goto(‘https://the-internet.herokuapp.com/javascript_alerts’);
  await page.locator(‘button:text(“Click for JS Confirm”)’).click();
});
//Handling Prompt
test(‘Handle Prompt’, async ({ page }) => {
  page.on(‘dialog’, async dialog => {
    console.log(dialog.message());
    await page.waitForTimeout(3000);
    await dialog.accept(‘Anand’);
    //await page.waitForTimeout(3000);
  });
  await page.goto(‘https://the-internet.herokuapp.com/javascript_alerts’);
  await page.locator(‘button:text(“Click for JS Prompt”)’).click();
});
Tags: ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!