我是 cypress 的新手,通过测试不同的方法来学习它。如果有人可以帮我解决以下问题
1.我了解到大多数 cypress 对象都使用 jquery 对象作为 DOM,但是它们之间到底有什么区别以及性能上会有什么差异? 2.我使用了cy.wrap函数,所以我可以使用should方法进行断言,否则我必须使用expect方法,任何导致何时使用哪一个?因为使用 then() 函数我不能使用 should 方法
对于 cy.wrap ,根据 cypress 文档,定义是“调用wrapp中主题上的函数并返回新值”,那么 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')
})
})
最佳答案
你问了很多问题,我什至不知道我是否完全理解它们,但让我们看看一些答案。
.should() vs .expect()
.should()
被认为是隐式断言并且 .expect()
明确的。有更详细的解释here 。 .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()
确实允许您继续链接 Cypress 命令,就像您对 .find()
所做的那样。有examples在文档中,看看。
关于javascript - Cypress对象与JQuery对象,cy.wrap函数的作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67762432/