Search code examples
typescriptplaywright

How can I use OR statements with Playwright assert


I want to check if the text content is either one or two:

await expect(this.header).toHaveText('one').or('two')

Is there a way to make it work using Playwright?

Also can this be implemented inside a sentence like

await expect(this.header).toHaveText("this is "+('first').or('second')+ "sentence");

Solution

  • Playwright v1.33 introduced a Locator.or that you can use like this:

    const headerOne = page.getByRole('heading', { name: 'One' });
    const headerTwo = page.getByRole('heading', { name: 'Two' });
    await expect(headerOne.or(headerTwo)).toBeVisible();