Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/src/api/class-locatorassertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,12 @@ CSS property name.

CSS property value.

### option: LocatorAssertions.toHaveCSS.pseudo
* since: v1.60
- `pseudo` <[PseudoElement]<"before"|"after">>

Pseudo-element to read computed styles from.

### option: LocatorAssertions.toHaveCSS.timeout = %%-js-assertions-timeout-%%
* since: v1.18

Expand Down
2 changes: 1 addition & 1 deletion packages/injected/src/injectedScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ export class InjectedScript {
matches: new ExpectedTextMatcher(options.expectedText[0]).matchesClassList(this, element.classList, /* partial */ expression === 'to.contain.class'),
};
} else if (expression === 'to.have.css') {
received = this.window.getComputedStyle(element).getPropertyValue(options.expressionArg);
received = this.window.getComputedStyle(element, options.pseudo ? `::${options.pseudo}` : undefined).getPropertyValue(options.expressionArg);
} else if (expression === 'to.have.id') {
received = element.id;
} else if (expression === 'to.have.text') {
Expand Down
1 change: 1 addition & 0 deletions packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2016,6 +2016,7 @@ scheme.FrameExpectParams = tObject({
selector: tOptional(tString),
expression: tString,
expressionArg: tOptional(tAny),
pseudo: tOptional(tEnum(['before', 'after'])),
expectedText: tOptional(tArray(tType('ExpectedTextValue'))),
expectedNumber: tOptional(tFloat),
expectedValue: tOptional(tType('SerializedArgument')),
Expand Down
6 changes: 3 additions & 3 deletions packages/playwright/src/matchers/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,17 @@ export function toHaveCount(
}, expected, options);
}

export function toHaveCSS(this: ExpectMatcherStateInternal, locator: LocatorEx, name: string, expected: string | RegExp, options?: { timeout?: number }): Promise<MatcherResult<any, any>>;
export function toHaveCSS(this: ExpectMatcherStateInternal, locator: LocatorEx, name: string, expected: string | RegExp, options?: { timeout?: number, pseudo?: 'before' | 'after' }): Promise<MatcherResult<any, any>>;
export function toHaveCSS(
this: ExpectMatcherStateInternal,
locator: LocatorEx,
name: string,
expected: string | RegExp,
options?: { timeout?: number },
options?: { timeout?: number, pseudo?: 'before' | 'after' },
) {
return toMatchText.call(this, 'toHaveCSS', locator, 'Locator', async (isNot, timeout) => {
const expectedText = serializeExpectedTextValues([expected]);
return await locator._expect('to.have.css', { expressionArg: name, expectedText, isNot, timeout });
return await locator._expect('to.have.css', { expressionArg: name, expectedText, isNot, pseudo: options?.pseudo, timeout });
}, expected, options);
}

Expand Down
5 changes: 5 additions & 0 deletions packages/playwright/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9223,6 +9223,11 @@ interface LocatorAssertions {
* @param options
*/
toHaveCSS(name: string, value: string|RegExp, options?: {
/**
* Pseudo-element to read computed styles from.
*/
pseudo?: "before"|"after";

/**
* Time to retry the assertion for in milliseconds. Defaults to `timeout` in `TestConfig.expect`.
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/src/channels.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3471,6 +3471,7 @@ export type FrameExpectParams = {
selector?: string,
expression: string,
expressionArg?: any,
pseudo?: 'before' | 'after',
expectedText?: ExpectedTextValue[],
expectedNumber?: number,
expectedValue?: SerializedArgument,
Expand All @@ -3481,6 +3482,7 @@ export type FrameExpectParams = {
export type FrameExpectOptions = {
selector?: string,
expressionArg?: any,
pseudo?: 'before' | 'after',
expectedText?: ExpectedTextValue[],
expectedNumber?: number,
expectedValue?: SerializedArgument,
Expand Down
7 changes: 5 additions & 2 deletions packages/protocol/src/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2951,6 +2951,11 @@ Frame:
selector: string?
expression: string
expressionArg: json?
pseudo:
type: enum?
literals:
- before
- after
expectedText:
type: array?
items: ExpectedTextValue
Expand Down Expand Up @@ -4586,5 +4591,3 @@ JsonPipe:
closed:
parameters:
reason: string?


14 changes: 14 additions & 0 deletions tests/page/expect-misc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,20 @@ test.describe('toHaveCSS', () => {
await expect(locator).toHaveCSS('color', 'rgb(255, 0, 0)');
});

test('should support pseudo element', async ({ page }) => {
await page.setContent(`
<style>
#node::before {
color: rgb(255, 0, 0);
content: "Text content";
}
</style>
<div id=node></div>
`);
const locator = page.locator('#node');
await expect(locator).toHaveCSS('color', 'rgb(255, 0, 0)', { pseudo: 'before' });
});

test('custom css properties', async ({ page }) => {
await page.setContent('<div id=node style="--custom-color-property:#FF00FF;">Text content</div>');
const locator = page.locator('#node');
Expand Down
Loading