Skip to content

Commit 48d58d2

Browse files
committed
review feedback
1 parent 88eb61e commit 48d58d2

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

docs/src/api/class-locatorassertions.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1443,19 +1443,21 @@ Ensures the [Locator] points to an element with given CSS classes. When a string
14431443
const locator = page.locator('#component');
14441444
await expect(locator).toHaveClass('middle selected row');
14451445
await expect(locator).toHaveClass('selected', { partial: true });
1446+
await expect(locator).toHaveClass('middle row', { partial: true });
14461447
```
14471448

14481449
```java
14491450
assertThat(page.locator("#component")).hasClass("middle selected row");
14501451
assertThat(page.locator("#component")).hasClass("selected", new LocatorAssertions.HasClassOptions().setPartial(true));
1452+
assertThat(page.locator("#component")).hasClass("middle row", new LocatorAssertions.HasClassOptions().setPartial(true));
14511453
```
14521454

14531455
```python async
14541456
from playwright.async_api import expect
14551457

14561458
locator = page.locator("#component")
14571459
await expect(locator).to_have_class("middle selected row")
1458-
await expect(locator).to_have_class("selected", partial=True)
1460+
await expect(locator).to_have_class("middle row", partial=True)
14591461
```
14601462

14611463
```python sync
@@ -1464,12 +1466,14 @@ from playwright.sync_api import expect
14641466
locator = page.locator("#component")
14651467
expect(locator).to_have_class("middle selected row")
14661468
expect(locator).to_have_class("selected", partial=True)
1469+
expect(locator).to_have_class("middle row", partial=True)
14671470
```
14681471

14691472
```csharp
14701473
var locator = Page.Locator("#component");
14711474
await Expect(locator).ToHaveClassAsync("middle selected row");
14721475
await Expect(locator).ToHaveClassAsync("selected", new() { Partial = true });
1476+
await Expect(locator).ToHaveClassAsync("middle row", new() { Partial = true });
14731477
```
14741478

14751479
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:

packages/playwright-core/src/server/injected/injectedScript.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1398,9 +1398,11 @@ export class InjectedScript {
13981398
return { received: null, matches: false };
13991399
received = value;
14001400
} else if (expression === 'to.have.class') {
1401+
if (!options.expectedText)
1402+
throw this.createStacklessError('Expected text is not provided for ' + expression);
14011403
return {
14021404
received: element.classList.toString(),
1403-
matches: new ExpectedTextMatcher(options.expectedText![0]).matchesClassList(element.classList, options.expressionArg.partial),
1405+
matches: new ExpectedTextMatcher(options.expectedText[0]).matchesClassList(this, element.classList, options.expressionArg.partial),
14041406
};
14051407
} else if (expression === 'to.have.css') {
14061408
received = this.window.getComputedStyle(element).getPropertyValue(options.expressionArg);
@@ -1455,7 +1457,7 @@ export class InjectedScript {
14551457
if (receivedClassLists.length !== options.expectedText.length)
14561458
return { received, matches: false };
14571459
const matches = this._matchSequentially(options.expectedText, receivedClassLists, (matcher, r) =>
1458-
matcher.matchesClassList(r, options.expressionArg.partial)
1460+
matcher.matchesClassList(this, r, options.expressionArg.partial)
14591461
);
14601462
return {
14611463
received: received,
@@ -1647,10 +1649,10 @@ class ExpectedTextMatcher {
16471649
return false;
16481650
}
16491651

1650-
matchesClassList(classList: DOMTokenList, partial: boolean): boolean {
1652+
matchesClassList(injectedScript: InjectedScript, classList: DOMTokenList, partial: boolean): boolean {
16511653
if (partial) {
16521654
if (this._regex)
1653-
throw new Error('Partial matching does not support regular expressions. Please provide a string value.');
1655+
throw injectedScript.createStacklessError('Partial matching does not support regular expressions. Please provide a string value.');
16541656
return this._string!.split(/\s+/g).filter(Boolean).every(className => classList.contains(className));
16551657
}
16561658
return this.matches(classList.toString());

packages/playwright/types/test.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8410,6 +8410,7 @@ interface LocatorAssertions {
84108410
* const locator = page.locator('#component');
84118411
* await expect(locator).toHaveClass('middle selected row');
84128412
* await expect(locator).toHaveClass('selected', { partial: true });
8413+
* await expect(locator).toHaveClass('middle row', { partial: true });
84138414
* ```
84148415
*
84158416
* When an array is passed, the method asserts that the list of elements located matches the corresponding list of

tests/page/expect-misc.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ test.describe('toHaveClass', () => {
232232
await expect(locator).not.toHaveClass('foo');
233233
await expect(locator).not.toHaveClass('foo', { partial: false });
234234
await expect(locator).toHaveClass(' bar foo ', { partial: true });
235+
await expect(locator).not.toHaveClass('does-not-exist', { partial: true });
235236
await expect(locator).not.toHaveClass(' baz foo ', { partial: true }); // Strip whitespace and match individual classes
236237
});
237238

@@ -241,7 +242,7 @@ test.describe('toHaveClass', () => {
241242
await expect(locator).toHaveClass(['aaa', 'b2b', 'ccc'], { partial: true });
242243
await expect(locator).not.toHaveClass(['aaa', 'b2b', 'ccc']);
243244
await expect(
244-
expect(locator).toHaveClass([/b2?ar/], { partial: true })
245+
expect(locator).toHaveClass([/b2?ar/, /b2?ar/, /b2?ar/], { partial: true })
245246
).rejects.toThrow('Partial matching does not support regular expressions. Please provide a string value.');
246247
await expect(locator).not.toHaveClass(['aaa', 'b2b', 'ccc'], { partial: false });
247248
await expect(locator).not.toHaveClass(['not-there', 'b2b', 'ccc'], { partial: true }); // Class not there

0 commit comments

Comments
 (0)