当计算得的宽度IS不准确时,如何在柏树中测试柏树中的宽度?
假设我在DOM中具有div
,'200px'
宽度和class
'target'
' 。
为了用柏树测试其宽度,我应该写下面的代码:
cy.get('.target').should('have.css', 'width', '200px');
还有很多测试,测试宽度和元素的高度...
...
今天发生了一些奇怪的事情!
所有这些测试都失败了,因为柏树发现的宽度
的值为200.00.000000000000032038799956012px
而不是200px
!!!
我想到的第一个解决方案是在实际数字上测试它们(ex,200
),而不是字符串(ex,'200px'
)成立;我认为这是一个昂贵的主意!
您认为我如何克服这个问题!?
Let's say I have a div
in the DOM with '200px'
of width and a class
of 'target'
.
In order to test its width with cypress, I should write the code below:
cy.get('.target').should('have.css', 'width', '200px');
and there's a bunch of tests, testing widths and heights of elements...
...
Today something weird happened!
All these tests have failed because the value of width
that cypress found was 200.0000000000032038879956012px
instead of 200px
!!!
The first solution that came into my mind was to test them on the actual numbers (ex, 200
), instead of the string (ex, '200px'
) that the cypress found; which I think is an expensive idea!
How do you think I can overcome this issue!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以匹配而无需任何转换,
You can match without any conversion,
测试断言
.should('have.css',...)
是 window.getCompentedStyle()在应用所有CSS后读取实际渲染值。它并不总是与以内联样式或样式表应用的“输入”设置相对应,具体取决于其他设置,例如理由,Flex设置,媒体查询。
从测试的角度来看,您可以切换到测试“输入”参数,
.should('have.attr','style','宽度:200px')
.should('have.class','target')
或者使用
chai-几乎
插件来检查关闭数字值。The test assertion
.should('have.css', ...)
is a wrapper for Window.getComputedStyle() which reads the actual rendered value after all CSS has been applied.It won't always correspond to the "input" setting applied in an inline style or via a style sheet, depending on other settings like justification, flex settings, media queries.
From a test perspective, you can switch to testing the "input" parameters,
.should('have.attr', 'style', 'width: 200px')
.should('have.class', 'target')
Alternatively use the
chai-almost
plugin to check close numeric values.您可以首先将200px转换为一个数字,然后检查数字是否在该范围内。像:
You can convert the 200px first into a number and then check that the number is in the range. Something like: