0

enter image description here

I have to check first the value of the status column with new, processing values to then be able to compare the dates.

if I have to compare a new vs processing data, the new date will be greater than the processing date. since the data is sorted by order first by value and then by date ascending. So if I find two values with the usual status I can make the following comparison:

    cy.get('#hometable > tbody > tr > td:nth-child(6)').each(($e, index, $list) => {
        if (index == 0){time_prev=0}
        time = Math.round(new Date($e.text()).getTime() / 1000)
        assert.isBelow(time_prev, time, 'previous date is below actual')
        time_prev = time
    })


 //so I take the value status
 cy.get('#hometable > tbody > tr > td:nth-child(5)').each(($e, index, $list) => {
        const text = $e.text()
        if (text.includes('new')) {
            expect(text).to.eq('\n                                                              new\n                                                           ')

        }
        if (text.includes('processing')) {
            expect(text).to.eq('\n                                                              processing\n                                                           ')
        }

    })

I should be able to make the two comparisons together in a single cycle but I have no idea where to start

i give following error:

enter image description here

4 Answers4

0

Original answer:

let arrayOfColumnOne = [];
let arrayOfColumnTwo = [];

cy.get('#hometable > tbody > tr > td:nth-child(6)').each(($e, index, $list) => {
    if (index == 0) {
        time_prev = 0
    }
    time = Math.round(new Date($e.text()).getTime() / 1000)
    assert.isBelow(time_prev, time, 'previous date is below actual')
    time_prev = time
    arrayOfColumnTwo.push(time)
})

//so I take the value status
cy.get('#hometable > tbody > tr > td:nth-child(5)').each(($e, index, $list) => {
    const text = $e.text()

    arrayOfColumnOne.push(text)
})
    .then(() => {
        for (let i = 1; i < arrayOfColumnOne; i++) {
            if (!arrayOfColumnTwo[i]) {
                throw new Error('Something is wrong with the array selectors')
            }
            if (arrayOfColumnOne[i].includes('new')) {
                expect(arrayOfColumnOne[i).to.eq('\n                                                              new\n                                                           ')
                assert.isBelow(arrayOfColumnTwo[i - 1], arrayOfColumntwo[i], 'previous date is below actual')
            } else if (text.includes('processing')) {
                expect(text).to.eq('\n                                                              processing\n                                                           ')
                //the assertion you need here
            } else {
                //some other code
            }
        }
    })

Editted with suggestion of using moment and cypress moment, because js round can be unstable

let arrayOfColumnTwo = [];

cy.get('#hometable > tbody > tr > td:nth-child(6)').each(($e, index, $list) => {
    cy.get($e)
        .parent()
        .find('td:nth-child(5)')
        .then($statusElement =>{
            if($statusElement.text().includes('new')) {
                expect($e.text()).to.eq('\n                                                              new\n                                                            ')
                assert.isBelow(arrayOfColumnTwo[i - 1], arrayOfColumnTwo[i], 'previous date is below actual')
            } else if($statusElement.text().includes('processing')){
                expect($e.text()).to.eq('\n                                                              processing\n                                                           ')
                assert.isAbove(arrayOfColumnTwo[i - 1], arrayOfColumntwo[i], 'previous date is below actual')
            }
        })

    time = Math.round(new Date($e.text()).getTime() / 1000)
    arrayOfColumnTwo.push(time)

})

edit 2:

let arrayOfColumnTwo = [];

cy.get('#hometable > tbody > tr > td:nth-child(6)').each(($e, index, $list) => {
    time = Math.round(new Date($e.text()).getTime() / 1000)
    arrayOfColumnTwo.push(time)
    cy.get($e)
        .parent()
        .find('td:nth-child(5)')
        .then($statusElement =>{
            if($statusElement.text().includes('new')) {
                expect($e.text()).to.eq('\n                                                              new\n                                                            ')
                assert.isBelow(arrayOfColumnTwo[i - 1], arrayOfColumnTwo[i], 'previous date is below actual')
            } else if($statusElement.text().includes('processing')){
                expect($e.text()).to.eq('\n                                                              processing\n                                                           ')
                assert.isAbove(arrayOfColumnTwo[i - 1], arrayOfColumntwo[i], 'previous date is below actual')
            }
        })



})
Rosen Mihaylov
  • 1,363
  • 2
  • 10
0

the first answer you gave was logically fine. I've made some changes. I noticed it doesn't enter the then loop

let arrayOfColumnOne = [];
let arrayOfColumnTwo = [];

cy.get('#hometable > tbody > tr > td:nth-child(6)').each(($e, index, $list) => {
    if (index == 0) {
        time_prev = 0
    }
    time = Math.round(new Date($e.text()).getTime() / 1000)
    arrayOfColumnTwo.push(time)

})

//so I take the value status
cy.get('#hometable > tbody > tr > td:nth-child(5)').each(($e, index, $list) => {
    const text = $e.text()
    arrayOfColumnOne.push(text)
}).then(() => {
    for (let i = 1; i < arrayOfColumnOne; i++) {
        if (!arrayOfColumnTwo[i]) {
            throw new Error('Something is wrong with the array selectors')
        }
        if (arrayOfColumnOne[i].includes('new') || arrayOfColumnOne[i].includes('processing')) {
            expect(arrayOfColumnOne[i]).to.eq('\n                                                              new\n                                                           ')
            assert.isBelow(arrayOfColumnTwo[i - 1], arrayOfColumntwo[i], 'previous date is below actual')
        } else if (arrayOfColumnOne[i].includes('processing') && arrayOfColumnOne[i - 1].includes('new')) {
            expect(text).to.eq('\n                                                              processing\n                                                           ')
            assert.isAbove(arrayOfColumnTwo[i - 1], arrayOfColumntwo[i], 'previous date is below actual')

        } else {
            //some other code
        }
    }
})

enter image description here

I don't understand why it doesn't enter the then

0
cy.log('startLoop').then(()=>{
            for (let i = 1; i < arrayOfColumnOne; i++) {
                if (!arrayOfColumnTwo[i]) {
                    throw new Error('Something is wrong with the array selectors')
                }
                if (arrayOfColumnOne[i].includes('new') && arrayOfColumnOne[i-1].includes('new')) {
                    expect(arrayOfColumnOne[i]).to.eq('\n                                                              new\n                                                           ')
                    assert.isBelow(arrayOfColumnTwo[i - 1], arrayOfColumntwo[i], 'previous date is below actual')
                } else if (arrayOfColumnOne[i].includes('processing') && arrayOfColumnOne[i - 1].includes('processing')) {
                    expect(text).to.eq('\n                                                              processing\n                                                           ')
                    assert.isBelow(arrayOfColumnTwo[i - 1], arrayOfColumntwo[i], 'previous date is below actual')
                } else {
                    expect(text).to.eq('\n                                                              processing\n                                                           ')
                    assert.isAbove(arrayOfColumnTwo[i - 1], arrayOfColumntwo[i], 'previous date is below actual')
                }
            }
        })

enter image description here

does not print anything

0
let arrayOfColumnTwo = [];

cy.get('#hometable > tbody > tr > td:nth-child(6)').each(($e, index, $list) => {
    cy.get($e)
        .parent()
        .find('td:nth-child(5)')
        .then($statusElement =>{
            if(index==0) {
            }
            else if($statusElement.text().includes('new')) {
                assert.isBelow(arrayOfColumnTwo[index - 1], arrayOfColumnTwo[index], 'previous date is below actual')
            } else{
                assert.isAbove(arrayOfColumnTwo[index - 1], arrayOfColumntwo[index], 'previous date is above actual')
            }
        })

    time = Math.round(new Date($e.text()).getTime() / 1000)
    arrayOfColumnTwo.push(time)

})

enter image description here

I have simplified the function so that it only compares dates. When it comes to compare the processing date of this error