-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #196
- Loading branch information
Showing
4 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
## Disallow using `getByTitle()` (`no-get-by-title`) | ||
|
||
The HTML `title` attribute does not provide a fully accessible tooltip for | ||
elements so relying on it to identify elements can hide accessibility issues in | ||
your code. This rule helps to prevent that by disallowing use of the | ||
`getByTitle` method. | ||
|
||
## Rule Details | ||
|
||
Example of **incorrect** code for this rule: | ||
|
||
```javascript | ||
await page.getByTitle('Delete product').click(); | ||
``` | ||
|
||
Example of **correct** code for this rule: | ||
|
||
```javascript | ||
await page.getByRole('button', { name: 'Delete product' }).click(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Rule } from 'eslint'; | ||
import { isPageMethod } from '../utils/ast'; | ||
|
||
export default { | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (isPageMethod(node, 'getByTitle')) { | ||
context.report({ messageId: 'noGetByTitle', node }); | ||
} | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Disallows the usage of getByTitle()', | ||
recommended: false, | ||
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-get-by-title.md', | ||
}, | ||
messages: { | ||
noGetByTitle: | ||
'The HTML title attribute is not an accessible name. Prefer getByRole() or getByLabelText() instead.', | ||
}, | ||
type: 'suggestion', | ||
}, | ||
} as Rule.RuleModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import rule from '../../src/rules/no-get-by-title'; | ||
import { runRuleTester, test } from '../utils/rule-tester'; | ||
|
||
const messageId = 'noGetByTitle'; | ||
|
||
runRuleTester('no-get-by-title', rule, { | ||
invalid: [ | ||
{ | ||
code: test('await page.getByTitle("lorem ipsum")'), | ||
errors: [{ column: 34, endColumn: 64, line: 1, messageId }], | ||
}, | ||
{ | ||
code: test('await this.page.getByTitle("lorem ipsum")'), | ||
errors: [{ column: 34, endColumn: 69, line: 1, messageId }], | ||
}, | ||
], | ||
valid: [ | ||
test('await page.locator("[title=lorem ipsum]")'), | ||
test('await page.getByRole("button")'), | ||
], | ||
}); |