[英]Is there a way to make check on HEX color - Cypress
I am currently busy with testing on Cypress.我目前正忙于在 Cypress 上进行测试。 I am actually new so i am not so familiar with everything around, but i am trying to test a CSS property of background-color
on certain element, but the problem is that behind the scenes everything is RGB, but i need to test on HEX.我实际上是新手,所以我对周围的一切都不太熟悉,但我正在尝试在某些元素上测试background-color
的 CSS 属性,但问题是幕后一切都是 RGB,但我需要在 HEX 上进行测试. So i ask myself is there a way to do that or a translation should be necessary?所以我问自己有没有办法做到这一点或者需要翻译?
cy.get('#button-login')
.should('have.css', 'background-color', "#6a7ba3")
ERROR : ...to have CSS property 'background-color' with the value '#6a7ba3', but the value was 'rgb(106, 123, 163)'
错误: ...to have CSS property 'background-color' with the value '#6a7ba3', but the value was 'rgb(106, 123, 163)'
You can achieve what you want using the chai-colors assertion plugin.您可以使用chai-colors断言插件来实现您想要的。
Install as follows:安装如下:
npm install chai-colors
Then add this to your code:然后将其添加到您的代码中:
import chaiColors from 'chai-colors'
chai.use(chaiColors)
Or this, as applicable:或者这个,如果适用的话:
var chaiColors = require('chai-colors');
chai.use(chaiColors);
Now you can write your assertion like this:现在你可以这样写你的断言:
cy.get('#button-login')
.should('have.css', 'background-color')
.and('be.colored', '#6a7ba3')
I was facing error with chai-colors
so I used the tinycolor2
library to assert the color as follows我遇到了chai-colors
的错误,所以我使用tinycolor2
库来断言颜色如下
import Toast from './index';
import { themedMount } from '../../../cypress/index';
import tinycolor from 'tinycolor2';
const toastColorMap: Record<string, string> = {
success: '#cff5c4',
error: '#fbcfc8',
};
const toasts = [
{ _id: '1', message: 'success toast notification', type: 'success' },
{ _id: '2', message: '2 success toast notification', type: 'error' },
{ _id: '3', message: 'error toast notification', type: 'error' },
];
describe('Toast.cy.tsx', () => {
it('Toast component renders toasts properly', () => {
themedMount(<Toast toasts={toasts} />);
toasts.forEach((t) => {
const toastElement = cy.contains(t.message);
toastElement.should('have.css', 'background-color').then((bg) => {
const color = tinycolor(bg);
expect(color.toHexString()).equal(toastColorMap[t.type]);
});
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.