Skip to content

fix(getByRole): name and exact#18719

Merged
dgozman merged 1 commit into
microsoft:mainfrom
dgozman:get-by-role-exact
Nov 11, 2022
Merged

fix(getByRole): name and exact#18719
dgozman merged 1 commit into
microsoft:mainfrom
dgozman:get-by-role-exact

Conversation

@dgozman
Copy link
Copy Markdown
Collaborator

@dgozman dgozman commented Nov 11, 2022

Following the getByText() and other methods:

  • By default, matching is substring and case-insensitive. Before, it was only case-insensitive, but not substring.
  • With new option exact: true, matching is full string and case-sensitive.
  • Matching always normalizes whitespace.
  • Codegen generates exact: false by default.
  • internal:role treats [name="foo"i] as non-exact match.

Various fixes:

  • Updated getByRole docs to match the reality.
  • Locator generator edge cases.

Copy link
Copy Markdown
Member

@pavelfeldman pavelfeldman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add locator-generator tests?

Comment thread packages/playwright-core/src/utils/isomorphic/locatorUtils.ts Outdated
Following the `getByText()` and other methods:

- By default, matching is substring and case-insensitive.
  Before, it was only case-insensitive, but not substring.
- With new option `exact: true`, matching is full string and case-sensitive.
- Matching always normalizes whitespace.
- Codegen generates `exact: false` by default.
- `internal:role` treats `[name="foo"i]` as non-exact match.

Various fixes:
- Updated `getByRole` docs to match the reality.
- Locator generator edge cases.
const accessibleName = getElementAccessibleName(element, includeHidden, hiddenCache).trim().replace(/\s+/g, ' ');
if (typeof nameAttr.value === 'string')
nameAttr.value = nameAttr.value.trim().replace(/\s+/g, ' ');
// internal:role assumes that [name="foo"i] also means substring.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can follow up and rename all 'i' to 'lax'

export type LocatorType = 'default' | 'role' | 'text' | 'label' | 'placeholder' | 'alt' | 'title' | 'test-id' | 'nth' | 'first' | 'last' | 'has-text' | 'has' | 'frame';
export type LocatorBase = 'page' | 'locator' | 'frame-locator';

type LocatorOptions = { attrs?: { name: string, value: string | boolean | number}[], exact?: boolean, name?: string | RegExp };
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attrs.name and name are clashing for me.

@dgozman dgozman merged commit a7b2b04 into microsoft:main Nov 11, 2022
aslushnikov pushed a commit that referenced this pull request Nov 14, 2022
Following the `getByText()` and other methods:

- By default, matching is substring and case-insensitive. Before, it was
only case-insensitive, but not substring.
- With new option `exact: true`, matching is full string and
case-sensitive.
- Matching always normalizes whitespace.
- Codegen generates `exact: false` by default.
- `internal:role` treats `[name="foo"i]` as non-exact match.

Various fixes:
- Updated `getByRole` docs to match the reality.
- Locator generator edge cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants