问题
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