Skip to content

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

fix(selectors): reset regex state before matching each element (#42818)

AbNoZ&v0222dc12271485
diff --git a/packages/injected/src/injectedScript.ts b/packages/injected/src/injectedScript.ts
index 636fb028ff..e7b0a29a90 100644
--- a/packages/injected/src/injectedScript.ts
+++ b/packages/injected/src/injectedScript.ts
@@ -1748,8 +1748,12 @@ function oneLine(s: string): string {
function createAttributeMatcher(part: AttributeSelectorPart): (s: string) => boolean {
const { value, caseSensitive } = part;
- if (value instanceof RegExp)
- return s => !!s.match(value);
+ if (value instanceof RegExp) {
+ return s => {
+ value.lastIndex = 0;
+ return !!s.match(value);
+ };
+ }
if (caseSensitive)
return s => s === value;
const lowerCaseValue = value.toLowerCase();
@@ -1775,7 +1779,11 @@ function createTextMatcher(selector: string, internal: boolean): { matcher: Text
if (selector[0] === '/' && selector.lastIndexOf('/') > 0) {
const lastSlash = selector.lastIndexOf('/');
const re = new RegExp(selector.substring(1, lastSlash), selector.substring(lastSlash + 1));
- return { matcher: (elementText: ElementText) => re.test(elementText.full), kind: 'regex' };
+ const matcher = (elementText: ElementText) => {
+ re.lastIndex = 0;
+ return re.test(elementText.full);
+ };
+ return { matcher, kind: 'regex' };
}
const unquote = internal ? JSON.parse.bind(JSON) : cssUnquote;
let strict = false;
@@ -1837,8 +1845,10 @@ class ExpectedTextMatcher {
return text === this._string;
if (this._substring !== undefined)
return text.includes(this._substring);
- if (this._regex)
+ if (this._regex) {
+ this._regex.lastIndex = 0;
return !!this._regex.test(text);
+ }
return false;
}
diff --git a/packages/injected/src/selectorEvaluator.ts b/packages/injected/src/selectorEvaluator.ts
index c02aa73c14..14cb489043 100644
--- a/packages/injected/src/selectorEvaluator.ts
+++ b/packages/injected/src/selectorEvaluator.ts
@@ -488,7 +488,10 @@ const textMatchesEngine: SelectorEngine = {
if (args.length === 0 || typeof args[0] !== 'string' || args.length > 2 || (args.length === 2 && typeof args[1] !== 'string'))
throw new Error(`"text-matches" engine expects a regexp body and optional regexp flags`);
const re = new RegExp(args[0], args.length === 2 ? args[1] : undefined);
- const matcher = (elementText: ElementText) => re.test(elementText.full);
+ const matcher = (elementText: ElementText) => {
+ re.lastIndex = 0;
+ return re.test(elementText.full);
+ };
return elementMatchesText((evaluator as SelectorEvaluatorImpl)._cacheText, element, matcher) === 'self';
},
};
diff --git a/packages/injected/src/selectorUtils.ts b/packages/injected/src/selectorUtils.ts
index 89ef0a1c90..b1fea88b33 100644
--- a/packages/injected/src/selectorUtils.ts
+++ b/packages/injected/src/selectorUtils.ts
@@ -35,8 +35,10 @@ export function matchesAttributePart(value: any, attr: AttributeSelectorPart) {
if (attr.op === '<truthy>')
return !!objValue;
if (attr.op === '=') {
- if (attrValue instanceof RegExp)
+ if (attrValue instanceof RegExp) {
+ attrValue.lastIndex = 0;
return typeof objValue === 'string' && !!objValue.match(attrValue);
+ }
return objValue === attrValue;
}
if (typeof objValue !== 'string' || typeof attrValue !== 'string')
diff --git a/tests/page/selectors-get-by.spec.ts b/tests/page/selectors-get-by.spec.ts
index de76065eca..1eca1ec308 100644
--- a/tests/page/selectors-get-by.spec.ts
+++ b/tests/page/selectors-get-by.spec.ts
@@ -375,3 +375,24 @@ it('getByRole with description whitespace normalization', async ({ page }) => {
'Alert',
]);
});
+
+it('locators should not be affected by global and sticky regex flags', async ({ page }) => {
+ await page.setContent(`
+ <div>foo</div><div>foo</div><div>foo</div>
+ <label>foo<input></label><label>foo<input></label><label>foo<input></label>
+ <button>foo</button><button>foo</button><button>foo</button>
+ <input placeholder=foo><input placeholder=foo><input placeholder=foo>
+ `);
+ await expect(page.getByText(/foo/)).toHaveCount(9);
+ await expect(page.getByText(/foo/g)).toHaveCount(9);
+ await expect(page.getByText(/foo/y)).toHaveCount(9);
+ await expect(page.getByLabel(/foo/)).toHaveCount(3);
+ await expect(page.getByLabel(/foo/g)).toHaveCount(3);
+ await expect(page.getByLabel(/foo/y)).toHaveCount(3);
+ await expect(page.getByRole('button', { name: /foo/ })).toHaveCount(3);
+ await expect(page.getByRole('button', { name: /foo/g })).toHaveCount(3);
+ await expect(page.getByRole('button', { name: /foo/y })).toHaveCount(3);
+ await expect(page.getByPlaceholder(/foo/)).toHaveCount(3);
+ await expect(page.getByPlaceholder(/foo/g)).toHaveCount(3);
+ await expect(page.getByPlaceholder(/foo/y)).toHaveCount(3);
+});