Skip to content

[Feature]: Allow toHaveCSS to target a pseudo-element #39873

Closed

Description

🚀 Feature Request

Allow toHaveCSS to target a pseudo-element like ::after or ::before. This could a new field, e.g. pseudoElement in the options parameter for toHaveCSS and would be a parallel to the second parameter of window.getComputedStyle(element, pseudoElement).

Example

expect(locator).toHaveCSS('background-color', 'red', { pseudo: '::before' });

Motivation

It's not always possible to put your dynamic CSS onto an element. For instance, I'm currently working with dynamic background images that can have a dynamic opacity level. It's not possible to do this in CSS at the moment, so I'm having to fake the background using a pseudo CSS ::before element. Then, in my Playwright tests, I have to evaluate():

const beforeStyle = locator.evaluate(el => window.getComputedStyle(el, '::before'));
expect(beforeStyle).toHaveProperty('opacity', opacity);

With this change, I could simply do this:

expect(locator).toHaveCSS('opacity', opacity, { pseudoElement: '::before' });

Since toHaveCSS is already using window.getComputedStyle() under the hood, this should be a relatively simple addition!

Activity

  1. dgozman commented on Apr 29, 2026

    @dgozman
    Collaborator

    Fixed by #40092.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions