question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] TestId Selector with regex does not find elements

See original GitHub issue

Context:

  • Playwright Version: 1.18.1
  • Operating System: MacOS
  • Node.js version: 16.13.1
  • Browser: All

Code Snippet

test.only("should find testid with regex inside", async ({ page }) => {
  await page.setContent(`<section data-testid="foo-09:00" >43543</section>`);
  await page.setContent(`<section data-testid="foo-14:00" >43543</section>`);

  const regex = new RegExp(`foo-[0-9]+:[0-9]+$`);
  const locator = await page.locator(`data-testid=${regex}`).count();
  console.log("locator count", locator); // returns 0
});

Describe the bug

I have a case with calendar component which has hour elements where some are disabled and some are not and I am trying to find first one that is not disabled. This works fine in Cypress, yet for Playwright returns 0 for the counts. Am I perhaps missing something?

Kind regards, Damevi

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
yury-scommented, Feb 4, 2022

Regular expressions currently don’t work for data-testid matcher, only text.

1reaction
ltsudacommented, Feb 4, 2022

Can you try the following?

const locator = await page.locator(`data-testid=/${regex}/`).count();

Search for the regex example here, you need the forward-slashes to use regex /regex/. https://playwright.dev/docs/selectors#text-selector

Maybe you need to set the regex const to be a string, without the new RegExp

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get the element with the testid using react testing library?
Show activity on this post. Hi i want to access the first div with class "icon" using getByTestId. As you see from above...
Read more >
About Queries | Testing Library
About Queries. Overview​. Queries are the methods that Testing Library gives you to find elements on the page. There are several types of ......
Read more >
Element Selectors | TestCafe Studio
Finds elements with an attribute of specific value. The method accepts both strings and regular expressions. For example, withAttribute('foo', ...
Read more >
Working with selectors | Checkly
Text selectors, allowing users to select an element based on its text ... they should enable you to reference most, if not all,...
Read more >
Queries | React Native Testing Library - Open Source
queryBy* queries return the first matching node for a query, and return null if no elements match. This is useful for asserting an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found