Rayrun
← Back to Discord Forum

How to use .filter() to narrow down the scope of the next chained locator?

Hi. In my HTML I ahve 6 sections that are have the same data-testid (variant-container). Each section has exactly one heading (unique) and one button. This test is green:

await expect(page
  .getByTestId('variant-container')
  .filter({ has: page.getByRole('heading', { name: 'Rec - Trial 7 days' }) })
).toHaveCount(1);

Indeed, the .filter() does its work and only keeps the section with the correct heading. But this test does not work:

await expect(page
  .getByTestId('variant-container')
  .filter({ has: page.getByRole('heading', { name: 'Rec - Trial 7 days' }) })
  .getByRole('button') // NEW LINE
).toHaveCount(1);

The count is 6! It's like the .filter() never happened and it finds all buttons in the 6 sections with the same data-testid.

Did I misunderstand how .filter() works?

This thread is trying to answer question "Did I misunderstand how `.filter()` works?"

1 reply

Never mind, looks like it works. I was using the wrong test-it and thus not selecting what I thought I was selectin…

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.