我是 Cypress.io 和 TypeScript 的新手。所以我不明白这里的一些东西。
My code:
//Test
describe('TEST description', function () {
it('newJobCreation', function () {
//Some code 1
var numberBefore = cy.get('#idOfItem')
var _numberBefore = +numberBefore
//Some code 2
var numberAfter = cy.get('#idOfItem')
var _numberAfter = +numberAfter
//Assertion
expect(_numberBefore-1).equals(_numberAfter) //Same result if I use: assert.equal(_numberBefore-1, _numberAfter)
})
})
假设 _numberBefore 之后 //Some code2 被更改并变为 _numberAfter。我想断言这个数字减少了 1。
在 Cypress.io 中运行测试后,我收到错误消息:
预期 NaN 等于 NaN
它失败了。
问题:
为什么我的断言在执行完所有代码后没有调用?为什么在测试开始时调用它?
Cypress 一次性对您的所有命令进行异步排队。这意味着
let elem = cy.get("#elem");
// attempt to do something with returned element...
不管用。cy.get()
只是告诉赛普拉斯添加get()
命令添加到最终要运行的命令列表中。它不会立即运行该命令。
.then()
提供了一个很好的替代方案 - 您可以使用它来排队一些要在命令运行时运行的 Javascript,如下所示:
cy.get("#elem1").then(elem1 => {
// elem1 is the underlying DOM object.
// You can put regular javascript code here:
console.log("This will happen when the queued .then() command is run");
// You can also put more Cypress commands here, like so:
cy.get("#elem2").should(elem2 => {
expect(elem1.someProperty).to.equal(elem2.someProperty);
});
});
注意.should(() => {})
行为就像.then()
,除非如果任何包含的断言失败,它将重试。
See here https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Closures有关相互比较两个元素的值的更多信息,请参阅此文档页面 https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html有关 Cypress 中异步命令队列的一般概念的更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)