|
| 1 | +## Disallow specific locator methods (`no-restricted-locators`) |
| 2 | + |
| 3 | +This rule bans specific locator methods from being used, and can suggest |
| 4 | +alternatives. |
| 5 | + |
| 6 | +## Rule Details |
| 7 | + |
| 8 | +Bans are expressed in the form of an array, where each element can be either: |
| 9 | + |
| 10 | +- A string: the method name to restrict (uses default message) |
| 11 | +- An object: `{ type: "methodName", message: "Custom message" }` (optional |
| 12 | + message) |
| 13 | + |
| 14 | +By default, the array is empty, meaning no locator methods are banned. |
| 15 | + |
| 16 | +For example: |
| 17 | + |
| 18 | +```json |
| 19 | +{ |
| 20 | + "playwright/no-restricted-locators": [ |
| 21 | + "error", |
| 22 | + [ |
| 23 | + "getByTestId", |
| 24 | + { |
| 25 | + "type": "getByTitle", |
| 26 | + "message": "Prefer getByRole or getByLabel instead" |
| 27 | + } |
| 28 | + ] |
| 29 | + ] |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +Examples of **incorrect** code for this rule with the above configuration: |
| 34 | + |
| 35 | +```javascript |
| 36 | +test('find button', async () => { |
| 37 | + await page.getByTestId('submit-button') |
| 38 | +}) |
| 39 | + |
| 40 | +test('find tooltip', async () => { |
| 41 | + await page.getByTitle('Additional info') |
| 42 | +}) |
| 43 | +``` |
| 44 | + |
| 45 | +Examples of **correct** code for this rule with the above configuration: |
| 46 | + |
| 47 | +```javascript |
| 48 | +test('find button', async () => { |
| 49 | + await page.getByRole('button', { name: 'Submit' }) |
| 50 | +}) |
| 51 | + |
| 52 | +test('find tooltip', async () => { |
| 53 | + await page.getByLabel('Additional info') |
| 54 | +}) |
| 55 | +``` |
| 56 | + |
| 57 | +## Options |
| 58 | + |
| 59 | +The rule accepts an array where each element can be: |
| 60 | + |
| 61 | +### String format |
| 62 | + |
| 63 | +A simple string with the method name to restrict: |
| 64 | + |
| 65 | +```json |
| 66 | +{ |
| 67 | + "playwright/no-restricted-locators": ["error", ["getByTestId", "getByTitle"]] |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### Object format |
| 72 | + |
| 73 | +An object with `type` (required) and optional `message`: |
| 74 | + |
| 75 | +```json |
| 76 | +{ |
| 77 | + "playwright/no-restricted-locators": [ |
| 78 | + "error", |
| 79 | + [ |
| 80 | + { |
| 81 | + "type": "getByTestId", |
| 82 | + "message": "Prefer getByRole or getByLabel instead" |
| 83 | + }, |
| 84 | + { "type": "getByTitle" } |
| 85 | + ] |
| 86 | + ] |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +### Mixed format |
| 91 | + |
| 92 | +You can mix string and object entries in the same array: |
| 93 | + |
| 94 | +```json |
| 95 | +{ |
| 96 | + "playwright/no-restricted-locators": [ |
| 97 | + "error", |
| 98 | + [ |
| 99 | + "getByTestId", |
| 100 | + { |
| 101 | + "type": "getByTitle", |
| 102 | + "message": "Prefer getByRole or getByLabel instead" |
| 103 | + } |
| 104 | + ] |
| 105 | + ] |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## Common Locator Methods |
| 110 | + |
| 111 | +Common locator methods that can be restricted: |
| 112 | + |
| 113 | +- `getByTestId` - Find elements by test ID attribute |
| 114 | +- `getByTitle` - Find elements by title attribute |
| 115 | +- `getByAltText` - Find elements by alt text |
| 116 | +- `getByPlaceholder` - Find elements by placeholder text |
| 117 | +- `getByLabel` - Find elements by label |
| 118 | +- `getByRole` - Find elements by ARIA role |
| 119 | +- `getByText` - Find elements by text content |
| 120 | + |
| 121 | +## When to Use This Rule |
| 122 | + |
| 123 | +This rule is useful when: |
| 124 | + |
| 125 | +- Your team wants to discourage certain locator methods (e.g., `getByTestId` for |
| 126 | + accessibility reasons) |
| 127 | +- You want to enforce consistent locator strategies across your test suite |
| 128 | +- You need to provide custom guidance on which alternatives to use |
0 commit comments