MichalG
MichalG

Reputation: 371

Cypress, JavaScript Varible is still equal 0 after increase it in .each method

Hi I've got cypress test like this, and I don't know why after .each method zeroCounter variable is still 0. I checked logs and in if block zeroCounter is equal 7 at the end of .each method. Could you help me with that?

it('TEST', () => {
    var zeroCounter = 0;
    cy.addDimensionByPropertyPanel('date');
    cy.exAddMeasureByPropertyPanel('data_with_nulls');
    cy.get('[tid="qv-object-VLC]').should('be.visible');
    cy.exShowValues();
    cy.exSelectDataAndGaps("Show as connections");
    cy.get('.vizlib-line-values')
        .each(($value) => {
            expect($value.text()).to.not.equal('-');
            if ($value.text() === "0.00") {
                zeroCounter++
                cy.log(zeroCounter);
            }
        })
    expect(zeroCounter).to.equal(7);
    cy.qsTakeScreenshot('Show null data as zero');
});

Upvotes: 0

Views: 235

Answers (1)

tacb
tacb

Reputation: 134

The problem is that Cypress don't execute the internal calls on demand, instead goes to a queue to be executed after. In your case the line expect(zeroCounter).to.equal(7); is executed before the .each(...), so the value is 0.

You need to use the expect inside then() to work:

cy.get('.vizlib-line-values')
.each(($value) => {
    ...
}).then(() => {
    expect(zeroCounter).to.equal(7);
});

Upvotes: 2

Related Questions