Skip to content

feat(prefer-locator): Add rule to suggest not using page methods #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ CLI option\
| [prefer-hooks-on-top](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | | |
| [prefer-lowercase-title](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | 🔧 | |
| [prefer-native-locators](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-native-locators.md) | Suggest built-in locators over `page.locator()` | | 🔧 | |
| [prefer-locator](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-locator.md) | Suggest built-in locators over page methods | | | |
| [prefer-locator](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-locator.md) | Suggest locators over page methods | | | |
| [prefer-strict-equal](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | | 💡 |
| [prefer-to-be](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-to-be.md) | Suggest using `toBe()` | | 🔧 | |
| [prefer-to-contain](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-to-contain.md) | Suggest using `toContain()` | | 🔧 | |
Expand Down
18 changes: 15 additions & 3 deletions docs/rules/prefer-locator.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Suggest using `page.locator()` (`prefer-locator`)

Instead of using page methods use locator-based e.g. page.fill() use
[locator.fill(value[, options])](https://playwright.dev/docs/api/class-locator#locator-fill)
Suggest using locators and their associated methods instead of page methods for
performing actions.

## Rule details

Expand All @@ -10,12 +10,24 @@ This rule triggers a warning if page methods are used, instead of locators.
The following patterns are considered warnings:

```javascript
page.click('css=button')
await page.click('css=button')
await page.dblclick('xpath=//button')
await page.fill('input[type="password"]', 'password')

await page.frame('frame-name').click('css=button')
```

The following pattern is **not** a warning:
The following pattern are **not** warnings:

```javascript
const locator = page.locator('css=button')
await page.getByRole('password').fill('password')
await page.getByLabel('User Name').fill('John')
await page.getByRole('button', { name: 'Sign in' }).click()
await page.locator('input[type="password"]').fill('password')
await page.locator('css=button').click()
await page.locator('xpath=//button').dblclick()

await page.frameLocator('#my-iframe').getByText('Submit').click()
```
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import preferComparisonMatcher from './rules/prefer-comparison-matcher'
import preferEqualityMatcher from './rules/prefer-equality-matcher'
import preferHooksInOrder from './rules/prefer-hooks-in-order'
import preferHooksOnTop from './rules/prefer-hooks-on-top'
import preferPageLocator from './rules/prefer-locator'
import preferLocator from './rules/prefer-locator'
import preferLowercaseTitle from './rules/prefer-lowercase-title'
import preferNativeLocators from './rules/prefer-native-locators'
import preferStrictEqual from './rules/prefer-strict-equal'
Expand Down Expand Up @@ -82,7 +82,7 @@ const index = {
'prefer-equality-matcher': preferEqualityMatcher,
'prefer-hooks-in-order': preferHooksInOrder,
'prefer-hooks-on-top': preferHooksOnTop,
'prefer-locator': preferPageLocator,
'prefer-locator': preferLocator,
'prefer-lowercase-title': preferLowercaseTitle,
'prefer-native-locators': preferNativeLocators,
'prefer-strict-equal': preferStrictEqual,
Expand Down
100 changes: 95 additions & 5 deletions src/rules/prefer-locator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,118 @@ runRuleTester('prefer-locator', rule, {
{
code: `
async function test() {
await page.fill();
await page.fill('input[type="password"]', 'password');
}
`,
errors: [
{
column: 15,
endColumn: 32,
column: 21,
endColumn: 68,
endLine: 3,
line: 3,
messageId: 'avoidAwaitPageMethods',
messageId: 'preferLocator',
},
],
output: null,
},
{
code: `
async function test() {
await page.dblclick('xpath=//button');
}
`,
errors: [
{
column: 21,
endColumn: 52,
endLine: 3,
line: 3,
messageId: 'preferLocator',
},
],
output: null,
},
{
code: `page.click('xpath=//button');`,
errors: [
{
column: 1,
endColumn: 29,
endLine: 1,
line: 1,
messageId: 'preferLocator',
},
],
output: null,
},
{
code: `
async function test() {
await page.frame('frame-name').click('css=button');
}
`,
errors: [
{
column: 21,
endColumn: 65,
endLine: 3,
line: 3,
messageId: 'preferLocator',
},
],
output: null,
},
{
code: `page.frame('frame-name').click('css=button')`,
errors: [
{
column: 1,
endColumn: 45,
endLine: 1,
line: 1,
messageId: 'preferLocator',
},
],
output: null,
},
],
valid: [
{
code: `const locator = page.locator('input[type="password"]')`,
},
{
code: `
async function test() {
await page.locator('input[type="password"]').fill('password');
}
`,
},
{
code: `
async function test() {
await page.locator();
await page.locator('xpath=//button').dblclick();
}
`,
},
{
code: `page.locator('xpath=//button').click();`,
},
{
code: `
async function test() {
await page.frameLocator('#my-iframe').locator('css=button').click();
}
`,
},
{
code: `
async function test() {
await page.evaluate('1 + 2');
}
`,
},
{
code: `page.frame('frame-name')`,
},
],
})
14 changes: 5 additions & 9 deletions src/rules/prefer-locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,12 @@ function isSupportedMethod(node: ESTree.CallExpression) {
export default createRule({
create(context) {
return {
AwaitExpression(node) {
// Must be a call expression
if (node.argument.type !== 'CallExpression') return

CallExpression(node) {
// Must be a method we care about
if (!isSupportedMethod(node.argument)) return
if (!isSupportedMethod(node)) return

context.report({
messageId: 'avoidAwaitPageMethods',
messageId: 'preferLocator',
node,
})
},
Expand All @@ -55,13 +52,12 @@ export default createRule({
meta: {
docs: {
category: 'Best Practices',
description: 'Discourage using await page methods',
description: 'Suggest locators over page methods',
recommended: false,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-locator.md',
},
messages: {
avoidAwaitPageMethods:
"Avoid using page methods e.g. 'await page.fill()', Use locator-based [locator.fill(value[, options])](https://playwright.dev/docs/api/class-locator#locator-fill)",
preferLocator: 'Prefer locator methods instead of page methods',
},
schema: [],
type: 'suggestion',
Expand Down