fix(selectors): reset regex state before matching each element - #42818
Conversation
Text and label matchers reuse one RegExp across elements. With the g or y flag, RegExp.prototype.test keeps lastIndex between calls, so every other matching element was skipped. Fixes microsoft#42813
|
Approach looks right, thanks. Two more sites reuse the regex across elements and are still stateful for
They use Please cover those too and add |
String.prototype.match resets lastIndex for the g flag but not for y, so getByRole name matching and attribute-based locators were still stateful across elements.
|
Thanks, good catch. Pushed a follow-up that resets |
Yury Semikhatsky (yury-s)
left a comment
There was a problem hiding this comment.
Drop the comments, they don't add anything.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Test results for "tests 1"8 flaky52085 passed, 1252 skipped Merge workflow run. |
Test results for "MCP"2 failed 8690 passed, 1474 skipped Merge workflow run. |
22dc122
into
microsoft:main
Summary
getByText,getByLabeland thetext-matchesengine reuse a singleRegExpacross elements. With thegoryflag,RegExp.prototype.testcarrieslastIndexbetween calls, so every other match was skipped (getByText(/foo/g)found 4 of 6 elements).lastIndexbefore each test, also in the expect text matcher, and add a test.Fixes #42813