How can I check if there are two web tabs open in Playwright?

Viewed 40

I was wondering if there is a way to verify using playwright that there are 2 web tabs open... In the code below I click on a button and verify that google has opened.

BUT!

I also would like to verify that there are now 2 tabs open and wanted to know if it can be done with some thing like .toHaveCount(2)

OR

would you guys recommend something better?

 test('Click button link test using blank target', async () => {
    const browser = await chromium.launch();
    const context = await browser.newContext();
    const page = await context.newPage();
    await page.goto(<my weblink>);
    const newPagePromise = new Promise((resolve) => context.once('page', resolve));

    // Button locator used to click the button to open the link
    const buttonLocator = page.locator('.MuiButton-root');

    // Click the button
    await buttonLocator.click();

    // Verify that after clicking on the page that the correct URL is present
    const newPage = await newPagePromise;
    expect(newPage.url()).toContain('https://www.google.ca');
  });

1 Answers

You can use browserContext.pages() to get all the pages opened in the context. You can then use length and assert the values like this

const totPages = context.pages().length
expect(totPages).toHaveCount(2);
Related