1

I have problem comparing with dates in cypress.

I have these dates

enter image description here

which are ordered in ascending order and the witness must demonstrate this.

With cypress I launch this test which compares that the dates are sorted in ascending order:

var time = 0 
var time_prev = 0   
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
            })

The test fails with the following error:

enter image description here

But as you can see from the first image, the second date is smaller than the third date.

I also notice that this test fails when I run multiple tests together.

bigeyes
  • 119
  • 1
  • 2
  • 11
  • I suggest to not use round - because JS is not the best mathematician. On the point of doing tests together - try separate the test case in its own file, so you can be sure no other case interferes with it. – Rosen Mihaylov Nov 12 '20 at 14:18
  • @bigeyes If you create two arrays one is created by pushing the each elements. Now we sort the same array and save it a new array. And then compare both of them, if they are same, all good, if not we throw an error, Is this something that can solves your use case ? – Alapan Das Nov 12 '20 at 14:35
  • @AlapanDas I just need to show that the dates are in ascending order. only that and the purpose of the test – bigeyes Nov 12 '20 at 14:38

1 Answers1

3

Here a good cypress solution for comparing dates and moment comapring dates, and a possible solution

var time = 0 
var time_prev = 0   
cy.get('#hometable > tbody > tr > td:nth-child(6)').each(($e, index, $list) => {
                if (index == 0){time_prev=0}
                time = $e.text()
                expect(Cypress.moment(time).isAfter(Cypress.moment(time_prev)).to.be.true;
                time_prev = time
            })
Rosen Mihaylov
  • 1,363
  • 2
  • 10
  • each function(){} TypeError time.isAfter is not a function cypress/integration/e2e_new_processing.spec.js:78:37 76 | if (index == 0){time_prev=0} 77 | time = $e.text() > 78 | expect(time.isAfter(time_prev)).to.be.true; | ^ 79 | time_prev = time 80 | }) 81 |. – bigeyes Nov 12 '20 at 14:34
  • I editted the suggestion - the links provide other solutions too – Rosen Mihaylov Nov 12 '20 at 14:37
  • ReferenceError moment is not defined – bigeyes Nov 12 '20 at 14:40
  • I don't understand how to define moment – bigeyes Nov 12 '20 at 15:04
  • It is defined as cypress utility https://docs.cypress.io/api/utilities/moment.html#See-also. Try using Cypress.moment on the places I used moment – Rosen Mihaylov Nov 12 '20 at 18:55