question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing color property always yields `rgb()` value, even when stored as hex

See original GitHub issue

Firstly, I ❤️ Cypress. Thank you so much for all of y’alls work.

Current behavior:

I have a quick test to ensure that some branding is working within a component:

cy.get('[data-testid="StepsCard"]')
  .find('h2')
  .should('have.css', 'color').and('equal', '#663399');

This is what the property looks like in the Cypress Test Runner’s browser’s dev tools: screen shot 2018-07-20 at 11 48 19 am

The test fails with the following error: screen shot 2018-07-20 at 11 51 58 am

I’ve also seen others complain about this behavior: https://gitter.im/cypress-io/cypress?at=5b4da9d88fe2780689bc20e7

Desired behavior:

Test passes when running yarn cypress:open, since the <h2> element within the StepsCard component does indeed have a color of #663399.

Steps to reproduce:

I’ll reproduce if absolutely necessary. I’m curious to know if perhaps this may be unavoidable. I understand y’all are very busy - feel free to close this issue if it isn’t up to the necessary standard 👍

Versions

  • macOS High Sierra v0.13.6
  • Chrome v67.0.3396.99
  • Cypress v3.0.1
  • React v16.4.1

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:19
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

32reactions
NicholasBollcommented, Mar 31, 2020

Perhaps try the following: https://gist.github.com/NicholasBoll/e584991b36986a85acf0e95101752bc0

The gist has the following JS to add to your cypress/support/commands.js file:

const compareColor = (color, property) => (targetElement) => {
    const tempElement = document.createElement('div');
    tempElement.style.color = color;
    tempElement.style.display = 'none'; // make sure it doesn't actually render
    document.body.appendChild(tempElement); // append so that `getComputedStyle` actually works
  
    const tempColor = getComputedStyle(tempElement).color;
    const targetColor = getComputedStyle(targetElement[0])[property];
  
    document.body.removeChild(tempElement); // remove it because we're done with it
  
    expect(tempColor).to.equal(targetColor);
};

Cypress.Commands.overwrite('should', (originalFn, subject, expectation, ...args) => {
    const customMatchers = {
        'have.backgroundColor': compareColor(args[0], 'backgroundColor'),
        'have.color': compareColor(args[0], 'color'),
    };
  
    // See if the expectation is a string and if it is a member of Jest's expect
    if (typeof expectation === 'string' && customMatchers[expectation]) {
        return originalFn(subject, customMatchers[expectation]);
    }
    return originalFn(subject, expectation, ...args);
});

You’ll then be able to do things like:

cy.get('button').should('have.color', 'black')
cy.get('button').should('have.color', '#000000')
cy.get('button').should('have.color', 'rgba(0, 0, 0)')

cy.get('button').should('have.backgroundColor', '#cccccc')

It works by using getComputedStyle on both the subject element and creates a temp element to get the computed color and compares. The assertion output could be better, but perhaps that’s an assertion plugin?

19reactions
kylemhcommented, Jul 20, 2018

Cool. I figured it had something to do with that old jQuery issue…

I - personally - would love to see that accepted as a core feature.

Curious to see how others feel.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ios - How do I pick the color in hexadecimal form or RGB form ...
A read-only computed property always returns a value, and can be accessed through dot ... green: 1, blue: 191/255, alpha: 1) } }...
Read more >
Color Formats in CSS - hex, rgb, hsl, lab - Josh W Comeau
CSS has a whole slew of different color formats: hex codes, rgb(), hsl(), ... still produce black, but 100% no longer always produces...
Read more >
How to Read Hex Color Codes - Hexadecimal Color | Pluralsight
RGB defines the values of red (the first number), green (the second number), or blue (the third number). The number 0 signifies no ......
Read more >
CSS Color Module Level 4 - W3C
This specification describes CSS <color> values, and properties for ... rgb() and its rgba() alias, which (like the hex color notation) ...
Read more >
Converting Color Spaces in JavaScript - CSS-Tricks
Converting RGB to hex is merely a change of radices. We convert the red, green, and blue values from decimal to hexadecimal using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found