2

enter image description here

I want to create some tests checking the styles of elements. We use these custom CSS vars. Is there any way to get these in cypress instead of checking for e.g. RGB(0,0,0)?

Thx in advance!

3 Answers 3

1

If you use cy.should() alongside have.css, you can specify which CSS property to check, and the value.

Using a simple example from your image, it would look something like this:

cy.get('foo')
  .should('have.css', 'min-width', '211px');

If there are more complex checks going on, you can always run the .should() as a callback.

cy.get('foo').should(($el) => {
  const minHeight = +($el.css('min-height').split('px')[0]);
  expect(minHeight).to.eql(40);
});

I found myself checking a lot of CSS values on elements, and opted to have a custom command that allowed me to pass in an expected object and check for all of those values.

Cypress.Commands.add('validateCss', { prevSubject: 'element' }, (subject, expected: { [key: string]: any }) => {
  Object.entries(expected).forEach(([key, value]) => {
    cy.wrap(subject).should('have.css', key, value);
  });
});

const expected = { 'min-width': '211px', 'min-height': '40px' };
cy.get('foo').validateCss(expected);
Sign up to request clarification or add additional context in comments.

3 Comments

thx, that is the solution I use so far. I wonder if I can also get these variables e.g. var(my-variable-for-background-color)
You'll get the resolved value, which could be formatted in a bunch of different ways. My recommendation would be to create your own helper function to parse a more human understandable expected to match the format returned. If you give me an example of what something like --buttonColor returns, I can try to come up with something.
I created a helper function to get the css: Cypress.Commands.add("getValueOfElementAttribute", (elem, attr, value) => { cy.log("get attribute " + attr + " with expected value" + value); cy.iframe().find(elem).invoke('attr', 'style', attr) .then(new_element => { expect(new_element).to.have.css(attr, value); }); }); but this only works with the actual value like e.g. assertexpected <button.jss5.jss6.> to have CSS property background-color with the value rgb(0, 115, 115)
-1

It is possible to evaluate a css variable fairly simply using getComputedStyle()

Cypress.Commands.add('cssVar', (cssVarName) => {
  return cy.document().then(doc => {
    return window.getComputedStyle(doc.body).getPropertyValue(cssVarName).trim()
  })
})

cy.cssVar('--mycolor')
  .should('eq', 'yellow')

where, for example

<html>
<head>
  <style>
    body { 
      --mycolor: yellow;
    }
    p {
      background-color: var(--mycolor);
    }
  </style>
</head>

But asserting that <p> has --mycolor requires a dummy element to evaluate yellow to rgb(255, 255, 0).

Cypress.Commands.add('hasCssVar', {prevSubject:true}, (subject, styleName, cssVarName) => {
  cy.document().then(doc => {
    const dummy = doc.createElement('span')
    dummy.style.setProperty(styleName, `var(${cssVarName})`)
    doc.body.appendChild(dummy)

    const evaluatedStyle = window.getComputedStyle(dummy).getPropertyValue(styleName).trim()
    dummy.remove()

    cy.wrap(subject)
      .then($el => window.getComputedStyle($el[0]).getPropertyValue(styleName).trim())
      .should('eq', evaluatedStyle)
  })
})

it('compares element property to CSS variable', () => {

  cy.cssVar('--mycolor').should('eq', 'yellow')

  cy.get('p').hasCssVar('background-color', '--mycolor') // passes

  cy.get('button').click()                               // change the css var color

  cy.cssVar('--mycolor').should('eq', 'red')  

  cy.get('p').hasCssVar('background-color', '--mycolor') // passes
})

The complication is not really because of the CSS var, but because we are dealing with color names that are automatically translated by the browser CSS engine.


Full test page

<html>
<head>
  <style>
    body { 
      --mycolor: yellow;
    }
    p {
      background-color: var(--mycolor);
    }
  </style>
</head>
<body>

  <p>Some text, change the background from yellow to red.</p>
  <button onclick="changeColor()">Change color</button>

  <script>
    function changeColor() { 
      document.body.style.setProperty('--mycolor', 'red')
    };
  </script>
</body>
</html>

Test log

enter image description here

2 Comments

I tried this solution, but when i add my var in the function it is always rgb(0,0,0). Not sure if it just isn't working for my webpage.
Can you say what implements the css? Looks like a pre or post processor, may be able to work a solution for it.
-2

Interacting with browser element or Dynamic CSS can be achieved in may ways ,

most use-full is cy.get() with the help of .should()

you can find here ( However i know you already checked this :) )

https://docs.cypress.io/api/commands/get#Get-vs-Find

for Example

cy.get('#comparison')
  .get('div')
  // finds the div.test-title outside the #comparison
  // and the div.feature inside
  .should('have.class', 'test-title')
  .and('have.class', 'feature')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.