Cypress.io + TypeScript.测试开始中的断言呼叫 [英] Cypress.io + TypeScript. Assertion call in beginning of test

查看:260
本文介绍了Cypress.io + TypeScript.测试开始中的断言呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cypress.io和TypeScript的新手.因此,我在这里不了解某些内容.

I am new to Cypress.io and TypeScript. So I do not understood some stuff here.

我的代码:

//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)
   }) 
})

让我们说//某些代码2更改后成为_numberAfter之后的_numberBefore.我想断言这个数字减少了1.

Lets say _numberBefore after //Some code2 was changed and become _numberAfter. I want to assert that number decreased by 1.

在Cypress.io中运行测试后,出现错误消息:

After I running test in Cypress.io, I am getting error message:

期望NaN等于NaN

expected NaN to equal NaN

它失败了.

问题:

为什么在所有代码执行完后我的断言没有调用?为什么在测试开始时调用它?

Why my assertion does not call after all code was executed? Why it was called in a beginning of test?

推荐答案

Cypress一次将所有命令异步排队.这意味着

Cypress asynchronously queues your commands all at once. This means that

let elem = cy.get("#elem");
// attempt to do something with returned element...

将不起作用. cy.get()只是告诉赛普拉斯将get()命令添加到最终要运行的命令列表中.它不会立即运行命令.

will not work. cy.get() just tells Cypress to add the get() command to the list of commands to eventually be run. It does not run the command right away.

.then()提供了一个不错的选择-您可以使用它来排队一些要在命令运行时运行的Javascript,例如:

.then() provides a nice alternative - you can use it to queue up some Javascript to be run when the command is run, like so:

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()相似,不同之处在于,如果任何包含的断言失败,它会重试.

Note that .should(() => {}) acts like a .then(), except it will retry if any contained assertions fail.

有关详细信息,请参见此处有关将两个元素的值相互比较的信息,请参见此文档页面了解有关赛普拉斯中异步命令排队的一般概念的更多信息.

See here for more info on comparing values of two elements to each other, and see this doc page for more info on the general concepts of asynchronous command queuing in Cypress.

这篇关于Cypress.io + TypeScript.测试开始中的断言呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆