Compare two values and make decision in Cypress

戏子无情 提交于 2019-12-24 19:53:09

问题


So I have two values on a page that I need to compare and as per the result perform some actions.

//First Block
cy.get('selctor1').invoke('text').then(somevalue => {
    cy.get('selector2').should('have.text', somevalue).then(() => {
        #Do Something when equal
    })
})

//Second Block
cy.get('selctor1').invoke('text').then(somevalue => {
    cy.get('selector2').should('not.have.text', somevalue).then(() => {
        #Do Something when not equal
    })
})

So for the positive case when both values are equal everything works fine. But for the case when two values are not equal, it's only checking the first block and fails. What should I do so that it executes the second block when values are not equal and not the first block?


回答1:


Sorry for not being clear the first time. Here is my edited answer:

Then vs Should:

Try to avoid then where possible. then is not repeatable and will introduce unexpected behaviour. But also will should introduce unexpeced behaviour.

Example for a bad usage of then:

describe("asd", () => {
    it("fails but retries", () =>{
        console.log("######### first test")

        cy.wrap({ fn: () => console.log(new Date())})
        .invoke("fn")
        .should(r => expect(r).to.eq(true));
    })


    it("fails but retries not", () =>{
        console.log("######### next test")

        cy.wrap({ fn: () => console.log(new Date())})
        .invoke("fn")
        .then(r => {
            expect(r).to.eq(true)
        });
    })
})

In this example you see the same code twice but the first block uses should while the second block uses then. The assertion must fail but in the first block, the assertion is repeated. Open the DEV COnsole to see many retries for the first block but no retry in the second.

This is what I mean by "unexpected" behaviour. Let's say, you wrap a object that is dynamically extended (maybe by a UI action) and you are expecting a property on this object. In the second block (then) the UI acton must be executed very fast and before thethenis executed so that theexpect` does not fail.

In the should case, you have 4 seconds (in case of `defaultCommandTimeout is not overwritten) left until the assert will finally fail.

Bad usage of should:

describe("ad", () => {
    it("test", () => {
        cy.visit("https://www.cypress.io/")
        cy.get("*[aria-label='pricing']")
            .invoke('text').should(someValue => { 
                cy.get("asdad", {timeout: 5000}).should("not.exist");
            })
    })
})

What would you expect? A green test? No, this test fails:

Why is this the case? Because get introduces an implicit assert "should exist" (see: https://docs.cypress.io/guides/core-concepts/introduction-to-cypress.html#Default-Assertions ).

Should with callback skips the default assertion (see: https://docs.cypress.io/api/commands/should.html#Notes ).I think they skip it by toggling it by flag. This could have the effect of reversing the flag again and thus forces cypress to check if "asdad" does exist even though we use should not exist.

There is an issue for this stuff: https://github.com/cypress-io/cypress/issues/5963

I do not know why cy.log has the behaviour you mentioned in your case. So either you use then if you want to use cy commands within then callback or you avoid the usage of cy commands and use should with explicit assertions (expect). Maybe after that issue is fixed, cy.log also can be used.

Old Answer:

    cy.get('selctor1').invoke('text').should(someValue => {
        const $el = Cypress.$('selector2');

        if ($el.text() ==== someValue) {
            // positive
            expect()....
        } else {
            // negative
            expect()....
        }
    })

You can use should with a callback. This callback (and the previous invoke command) is executed as long as the timeout is reached or no assertion fails. You always can use the raw jQuery object to work with. This depends on whether or not you need all the checks cypress is executing during a get().

Please let me know if you need further assistance.



来源:https://stackoverflow.com/questions/59317965/compare-two-values-and-make-decision-in-cypress

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