In Playwright I want to check if a click triggers a popup.
I thought I could do it this way: Define a promise, click and wait for the promise in a try/catch. This works as expected. (In my example I removed the handling of the dialog and the error).
My problem is, that in case the promise is not met, my page seems to be closed (but it stays open in the browser). I get: ‘Error: locator.innerText: Target page, context or browser has been closed.’ for the second console.log (but not for waitForLoadState(‘networkidle’)).
What can I do to access page again?
import { test, expect } from '@playwright/test';
test('click, catching a non existing popup', async({ page }) => {
await page.goto('/');
await page.waitForLoadState('networkidle');
console.log('************ Original page ***************');
console.log( await page.locator('#maintitle').innerText());
const dlg2Promise = page.waitForEvent('popup');
await page.getByRole('button',{name: 'Referenzen'}).click();
try {
dlg2 = await dlg2Promise;
} catch (error) {
// Ignore if dlg2 does not appear
}
await page.waitForLoadState('networkidle');
console.log('*********** Page already here? ****************');
console.log( await page.locator('#maintitle').innerText());
});