首页 > 解决方案 > Cypress 对象 vs JQuery 对象,cy.wrap 函数的作用

问题描述

我是柏树的新手,通过测试不同的方法来学习它。如果有人可以帮助我解决以下问题

1.我了解到大多数 cypress 对象都使用 DOM 的 jquery 对象,但它们之间到底有什么区别,在性能上会有什么区别?2.我使用了cy.wrap函数,所以我可以使用should方法进行断言,否则我必须使用expect方法,有什么线索什么时候使用哪一个??因为使用 then() 函数我不能使用 should 方法

对于 cy.wrap ,根据 cypress 文档,定义是“在 wrap 中调用主题上的函数并返回新值”, cy.wrap 的作用到底是什么?

在下面的示例代码中,我同时使用了 should() 和 expect() 进行断言

提前致谢。

     cy.contains('nb-card','Using the Grid').then(firstForm =>{
            const emailLabelFirst = firstForm.find('[for="inputEmail1"]').text()
            const passwordLabelFirst = firstForm.find('[for="inputPassword2"]').text()
            // jquery object assertion is done cia expect method
            expect(emailLabelFirst).to.equals('Email')
            expect(passwordLabelFirst).to.equals('Password')

            cy.contains('nb-card','Basic form').then(secondForm=>{

                const passwordLabelSecond = secondForm.find('[for="exampleInputPassword1"]').text()
                expect(passwordLabelFirst).to.equal(passwordLabelSecond)
               // cy.log('This is test logging message')

               // with cypress object assertion is done via should method
               cy.wrap(secondForm).find('[for="exampleInputPassword1"]').should('contain','Password')
                
            })
           
            
           
        })

标签: javascriptjquerycypress

解决方案


你问了很多问题,我什至不知道我是否都理解得很好,但让我们看看一些答案。

.should() 与 .expect()

.should()被认为是隐式断言和.expect()显式断言。这里有更详细的解释。.should()很棒,因为它等待并重试,这是您无法实现的.expect(),也无法使用.then().

通常,.expect()当您想对同一主题执行多个断言时使用。就像您想断言响应状态代码是 200 并且响应正文中有一些特定属性:

cy
  .request(url)
  .then(res => {
    expect(res.statusCode).to.equal(200);
    expect(response.body).to.have.property('name', 'pavel');
  });

cy.wrap()

cy.wrap()确实允许您继续链接赛普拉斯命令,就像您使用.find(). 文档中有示例,请查看。


推荐阅读