Skip to content

Instantly share code, notes, and snippets.

@NicholasBoll
Last active November 8, 2023 15:50
Show Gist options
  • Save NicholasBoll/e584991b36986a85acf0e95101752bc0 to your computer and use it in GitHub Desktop.
Save NicholasBoll/e584991b36986a85acf0e95101752bc0 to your computer and use it in GitHub Desktop.
Cypress assertion to compare colors
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);
});
const compareColor = (color: string, property: keyof CSSStyleDeclaration) => (
targetElement: JQuery
) => {
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: Function, subject: any, expectation: any, ...args: any[]) => {
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);
});
@Gurumoorthyb
Copy link

No, We haven't changed the atlas incoming color for ex: #455D82 if this is the color in the atlas in UI it will render as same RGB(69, 93, 130)

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