-
Notifications
You must be signed in to change notification settings - Fork 4.3k
chore: add toHaveClass partial option #35229
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -1431,7 +1431,7 @@ Attribute name. | |
* langs: | ||
- alias-java: hasClass | ||
|
||
Ensures the [Locator] points to an element with given CSS classes. When a string is provided, it must fully match the element's `class` attribute. To match individual classes or perform partial matches, use a regular expression: | ||
Ensures the [Locator] points to an element with given CSS classes. When a string is provided, it must fully match the element's `class` attribute. To match individual classes or perform partial matches use [`option: LocatorAssertions.toHaveClass.partial`]. | ||
|
||
**Usage** | ||
|
||
|
@@ -1442,34 +1442,38 @@ Ensures the [Locator] points to an element with given CSS classes. When a string | |
```js | ||
const locator = page.locator('#component'); | ||
await expect(locator).toHaveClass('middle selected row'); | ||
await expect(locator).toHaveClass(/(^|\s)selected(\s|$)/); | ||
await expect(locator).toHaveClass('selected', { partial: true }); | ||
await expect(locator).toHaveClass('middle row', { partial: true }); | ||
``` | ||
|
||
```java | ||
assertThat(page.locator("#component")).hasClass(Pattern.compile("(^|\\s)selected(\\s|$)")); | ||
assertThat(page.locator("#component")).hasClass("middle selected row"); | ||
assertThat(page.locator("#component")).hasClass("selected", new LocatorAssertions.HasClassOptions().setPartial(true)); | ||
assertThat(page.locator("#component")).hasClass("middle row", new LocatorAssertions.HasClassOptions().setPartial(true)); | ||
``` | ||
|
||
```python async | ||
from playwright.async_api import expect | ||
|
||
locator = page.locator("#component") | ||
await expect(locator).to_have_class(re.compile(r"(^|\\s)selected(\\s|$)")) | ||
await expect(locator).to_have_class("middle selected row") | ||
await expect(locator).to_have_class("middle row", partial=True) | ||
``` | ||
|
||
```python sync | ||
from playwright.sync_api import expect | ||
|
||
locator = page.locator("#component") | ||
expect(locator).to_have_class(re.compile(r"(^|\\s)selected(\\s|$)")) | ||
expect(locator).to_have_class("middle selected row") | ||
expect(locator).to_have_class("selected", partial=True) | ||
expect(locator).to_have_class("middle row", partial=True) | ||
``` | ||
|
||
```csharp | ||
var locator = Page.Locator("#component"); | ||
await Expect(locator).ToHaveClassAsync(new Regex("(^|\\s)selected(\\s|$)")); | ||
await Expect(locator).ToHaveClassAsync("middle selected row"); | ||
await Expect(locator).ToHaveClassAsync("selected", new() { Partial = true }); | ||
await Expect(locator).ToHaveClassAsync("middle row", new() { Partial = true }); | ||
``` | ||
|
||
When an array is passed, the method asserts that the list of elements located matches the corresponding list of expected class values. Each element's class attribute is matched against the corresponding string or regular expression in the array: | ||
|
@@ -1523,6 +1527,12 @@ Expected class or RegExp or a list of those. | |
|
||
Expected class or RegExp or a list of those. | ||
|
||
### option: LocatorAssertions.toHaveClass.partial | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A topic for the API review: does |
||
* since: v1.52 | ||
- `partial` <[boolean]> | ||
|
||
Whether to perform a partial match, defaults to `false`. In an exact match, which is the default, the `className` attribute must be exactly the same as the asserted value. In a partial match, all classes from the asserted value, separated by spaces, must be present in the [Element.classList](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList) in any order. Partial match does not support a regular expression. | ||
|
||
### option: LocatorAssertions.toHaveClass.timeout = %%-js-assertions-timeout-%% | ||
* since: v1.18 | ||
|
||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.