Is there a way in which I can test the content
of the pseudo CSS class for :before on my element with Cypress?
I have seen links documenting:
There's a way to assert on the CSS properties of pseudo-elements, although it's not as simple as just using a Cypress command.
cy.get()
to get a reference to the element.Window
object off of the element, and then invoke Window.getComputedStyle(), which can read the computed CSS of pseudo selectors.content
property.Here's an example that works on the code posted in the OP:
cy.get('.myClass')
.then($els => {
// get Window reference from element
const win = $els[0].ownerDocument.defaultView
// use getComputedStyle to read the pseudo selector
const before = win.getComputedStyle($els[0], 'before')
// read the value of the `content` CSS property
const contentValue = before.getPropertyValue('content')
// the returned value will have double quotes around it, but this is correct
expect(contentValue).to.eq('"foo-"')
})