Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(locator-generator): keep regexes with u, s, y, d and v flags (#42819)

AbNoZ&v0256c0b49f379c
diff --git a/packages/isomorphic/locatorGenerators.ts b/packages/isomorphic/locatorGenerators.ts
index 7ac621c2cf..4aec1339aa 100644
--- a/packages/isomorphic/locatorGenerators.ts
+++ b/packages/isomorphic/locatorGenerators.ts
@@ -291,7 +291,7 @@ function combineTokens(factory: LocatorFactory, tokens: string[][], maxOutputSiz
function detectExact(text: string): { exact?: boolean, text: string | RegExp } {
let exact = false;
- const match = text.match(/^\/(.*)\/([igm]*)$/);
+ const match = text.match(/^\/(.*)\/([dgimsuvy]*)$/);
if (match)
return { text: new RegExp(match[1], match[2]) };
if (text.endsWith('"')) {
diff --git a/tests/library/locator-generator.spec.ts b/tests/library/locator-generator.spec.ts
index 6921d553ce..4476a8c265 100644
--- a/tests/library/locator-generator.spec.ts
+++ b/tests/library/locator-generator.spec.ts
@@ -113,6 +113,15 @@ it('reverse engineer locators', async ({ page }) => {
javascript: 'getByLabel(/Last\\s+name/i)',
python: 'get_by_label(re.compile(r"Last\\s+name", re.IGNORECASE))',
});
+ // Only JavaScript can express these flags, other languages drop them.
+ for (const flags of ['u', 's', 'y', 'd', 'v', 'gm']) {
+ for (const [engine, method] of [['text', 'getByText'], ['label', 'getByLabel']]) {
+ const selector = `internal:${engine}=/Hello/${flags}`;
+ const locatorString = asLocator('javascript', selector);
+ expect.soft(locatorString, selector).toBe(`${method}(/Hello/${flags})`);
+ expect.soft(parseLocator('javascript', locatorString, 'data-testid'), selector).toBe(selector);
+ }
+ }
expect.soft(generate(page.getByPlaceholder('hello'))).toEqual({
csharp: 'GetByPlaceholder("hello")',