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
# AG0042: QuerySelector should not be used with Playwright
2
+
3
+
## Problem Description
4
+
5
+
Using `QuerySelectorAsync()` in Playwright tests can lead to brittle and unreliable tests. This method uses CSS selectors which can be fragile and may break when the UI structure changes. Instead, more reliable locator strategies like data-testid or role-based selectors should be used.
6
+
7
+
## Rule Details
8
+
9
+
This rule raises an issue when `QuerySelectorAsync()` is called on Playwright `IPage` or `Page` objects.
10
+
11
+
### Noncompliant Code Example
12
+
13
+
```csharp
14
+
publicasyncTaskClickLoginButton(IPagepage)
15
+
{
16
+
// Noncompliant: Using QuerySelectorAsync with CSS selector
1.**Fragile Selectors**: CSS selectors are tightly coupled to the DOM structure and styling classes, making tests brittle when:
41
+
- CSS classes are renamed or removed
42
+
- DOM hierarchy changes
43
+
- Styling frameworks are updated
44
+
45
+
2.**Maintainability**: CSS selectors can be complex and hard to maintain, especially when dealing with nested elements or specific combinations of classes.
46
+
47
+
3.**Best Practices**: Playwright provides better alternatives that are:
48
+
- More resilient to changes
49
+
- More readable and maintainable
50
+
- Better aligned with testing best practices
51
+
52
+
## Better Alternatives
53
+
54
+
Playwright provides several better methods for selecting elements:
0 commit comments