Cypress.io + TypeScript. Assertion call in beginning of test

非 Y 不嫁゛ 提交于 2019-12-11 01:00:00

问题


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

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

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

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

expected NaN to equal NaN

and it fails.

Question:

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


回答1:


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

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

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() 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);
    });
});

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.



来源:https://stackoverflow.com/questions/51749907/cypress-io-typescript-assertion-call-in-beginning-of-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!