Rayrun
← Back to Discord Forum

Intermittent errors attempting to verify page title after navigating

I am really new to playwright so I am not sure if I am doing something fundamentally wrong. I am writing some basic website navigation tests and I cannot for the life of me get them to pass reliably across all 5 default projects that are setup when playwright/test is installed. I have read all of the basics on the website and have tried implementing the best practices when navigating.

type MyFixtures = {
    navHeader: Header;
};
const test = base.extend<MyFixtures>({
    navHeader: async ({ page }, use) => {
        await page.goto('');
        const navHeader = new Header(page);
        await use(navHeader);
    },
});
test.describe('Header Menu Navigation Tests', () => {
    test('Navigate to Data Protection page', async ({ navHeader }) => {
        const dpPage = await navHeader.nav.dataProtection();
        expect(dpPage).toHaveTitle('Data Protection');
    });

The supporting code looks like:

export class Header {
    readonly page: Page;
    readonly nav: NavMenu;

    constructor(page: Page) {
        this.page = page;
        this.nav = new NavMenu(page);
}
export class NavMenu {
    readonly page: Page;
    readonly dataProtectionProdItem: Locator;

    constructor(page: Page) {
        this.page = page;
        this.dataProtectionProdItem = page.getByTestId('nav-data-protection');
    }
    async dataProtection() {
        await this.gotoMenuItem(this.dataProtectionProdItem, this.productDropdown);
        await this.page.waitForURL('**/data-protection');
        await this.page.getByRole('heading', { name: 'Data Protection' }).waitFor();
        return this.page;
    }
}

A captured trace shows that the correct page is navigated to, an element on the page is found, the title even appears when I pop out the page from the trace viewer...but then on the next action to verify the title the browser url shows "about:blank". The teardown then shows the actual page which is confusing. It's almost like playwright opens a new window for the fail

This thread is trying to answer question "Why am I getting intermittent errors when trying to verify the page title after navigating in Playwright?"

1 reply

I figured it out. I had forgot to put await before my assertions.... what a node newbie.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.