转换应表达为软主张

发布于 2025-02-12 14:49:14 字数 530 浏览 0 评论 0原文

我有一个复杂的形式,具有许多单独的功能可以测试,我想在整个页面上遍历整个页面而不会失败。我已经看到了软主张,但无法弄清楚如何使其与禁用之类的属性一起使用。

例如

<input data-id="name"></input>
<input data-id="email"></input>
<input data-id="password" disabled></input>

cy.get('[data-id="name"]').should('not.be.disabled') 
cy.get('[data-id="email"]').should('be.disabled')            // fails - convert to soft assertion?
cy.get('[data-id="password"]').should('have.attr', 'disabled') 

I have a complex form with many individual features to test, and I'd like to traverse the whole page without failing the test. I've seen soft assertions but cannot figure out how to make it work with attributes such as disabled.

For example

<input data-id="name"></input>
<input data-id="email"></input>
<input data-id="password" disabled></input>

cy.get('[data-id="name"]').should('not.be.disabled') 
cy.get('[data-id="email"]').should('be.disabled')            // fails - convert to soft assertion?
cy.get('[data-id="password"]').should('have.attr', 'disabled') 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冷心人i 2025-02-19 14:49:14

您可以使用 alfonso-presa/soft-assert 有效。

这意味着您必须将.should()的样式更改为回调版。

另外,由于Expect()在表达式为false时不再失败,因此您将失去重试功能。仅在稳定的HTML页面上使用它。

const { proxy, flush } = require("@alfonso-presa/soft-assert");
expect = proxy(expect);

it('tests by soft assert', () => {

  cy.get('[data-id="name"]').should($el => expect($el).not.to.be.disabled) 

  // shows failed in log but test continues
  cy.get('[data-id="email"]').should($el => expect($el).to.be.disabled)  

  cy.get('[data-id="password"]').should($el => expect($el).to.have.attr('disabled')) 

  cy.then(flush)  // flush any errors
})

注意
flush()方法应添加到柏树命令链中,否则测试在评估之前结束。

You could use alfonso-presa/soft-assert to change the way expect works.

It would mean you have to alter the style of .should() to the callback version.

Also, since the expect() not longer fails when the expression is false, you lose retry capability. Only use this on a stable HTML page.

const { proxy, flush } = require("@alfonso-presa/soft-assert");
expect = proxy(expect);

it('tests by soft assert', () => {

  cy.get('[data-id="name"]').should($el => expect($el).not.to.be.disabled) 

  // shows failed in log but test continues
  cy.get('[data-id="email"]').should($el => expect($el).to.be.disabled)  

  cy.get('[data-id="password"]').should($el => expect($el).to.have.attr('disabled')) 

  cy.then(flush)  // flush any errors
})

Note
The flush() method should be added to Cypress command chain, otherwise the test ends before it is evaluated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文