You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When choosing locators for test code, follow this priority order (best to worst). Always prefer the highest-priority strategy that uniquely identifies the element.
7
+
When choosing locators for test code, follow this priority order (best to worst). **Prefer unique CSS or XPath with stable attributes over text-based locators** so that when a check fails you can tell quickly whether the element is missing (bug) or the copy changed (new functionality / locale).
8
8
9
9
### 1. `data-testid` attributes (Best)
10
10
@@ -23,8 +23,11 @@ Custom data attributes that carry stable, semantic meaning.
awaitexpectElementToContainText(orderCompleteMessage(),/thank you for your order/i);
85
+
```
86
+
87
+
### 7. Playwright built-in locators (role / text) — use only when no stable selector exists
88
+
89
+
Text- and role-based locators are **flaky**: they change with copy, locale, and country. If the only way to find an element is by its text, a failure does not tell you whether the element is missing (bug) or the wording changed (new feature / i18n). Prefer **data-testid**, **data-\***, **id**, or **unique CSS/XPath** first.
// By visible text — avoid for assertions; use stable selector + assert text separately
105
+
awaitclick(getLocatorByText('Add to cart'));
106
+
```
107
+
108
+
**Assertions:** Prefer a **stable locator** for the element and assert the **text in the assertion**. That way a failure shows "expected text X, got Y" (copy change) vs element not found (bug).
Copy file name to clipboardExpand all lines: package.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
{
2
2
"name": "playwright-ts-template",
3
-
"version": "1.24.0",
3
+
"version": "1.24.1",
4
4
"description": "Playwright Type script framework is designed for Web (Desktop & Mobile), API, and Electron apps. Stable and Robust layer on top of Playwright with inbuilt Utilities, Linting, Logging, Web hooks, Github actions, Reports and much more",
0 commit comments