当计算得的宽度IS不准确时,如何在柏树中测试柏树中的宽度?

发布于 2025-02-02 02:53:09 字数 575 浏览 4 评论 0原文

假设我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬﹪忆之殇 2025-02-09 02:53:09

您可以匹配而无需任何转换,

cy.get('div')
  .invoke('css', 'width')
  .should('match', /200.*px/)   // matches 200.0000000000032038879956012px or 200px

You can match without any conversion,

cy.get('div')
  .invoke('css', 'width')
  .should('match', /200.*px/)   // matches 200.0000000000032038879956012px or 200px
眼中杀气 2025-02-09 02:53:09

测试断言.should('have.css',...) window.getCompentedStyle()在应用所有CSS后读取实际渲染值。

它并不总是与以内联样式或样式表应用的“输入”设置相对应,具体取决于其他设置,例如理由,Flex设置,媒体查询。

从测试的角度来看,您可以切换到测试“输入”参数,

  • 用于内联样式
    .should('have.attr','style','宽度:200px')
  • for Style Sheets

    .should('have.class','target')


或者使用chai-几乎插件来检查关闭数字值。

const chaiAlmost = require('chai-almost');
chai.use(chaiAlmost(1))                           // 1 pixel variance

cy.get('.target')
  .invoke('css', 'width')                         // extract computed width 
  .then(widthStr => +widthStr.replace('px', ''))  // convert to number
  .should('almost.equal', 200);                   // approximate 200 +/- 1 px

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,

  • for inline styles
    .should('have.attr', 'style', 'width: 200px')
  • for style sheets
    .should('have.class', 'target')

Alternatively use the chai-almost plugin to check close numeric values.

const chaiAlmost = require('chai-almost');
chai.use(chaiAlmost(1))                           // 1 pixel variance

cy.get('.target')
  .invoke('css', 'width')                         // extract computed width 
  .then(widthStr => +widthStr.replace('px', ''))  // convert to number
  .should('almost.equal', 200);                   // approximate 200 +/- 1 px
若沐 2025-02-09 02:53:09

您可以首先将200px转换为一个数字,然后检查数字是否在该范围内。像:

cy.get('.target')
  .invoke('css', 'width')
  .then((width) => {
    expect(+width.replace('px', '')).to.be.within(200, 201)
  })

You can convert the 200px first into a number and then check that the number is in the range. Something like:

cy.get('.target')
  .invoke('css', 'width')
  .then((width) => {
    expect(+width.replace('px', '')).to.be.within(200, 201)
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文