0

I am trying some UI tests on Cypress and would like to continue this discussion mentioned here in SO.

I have a table like this but would like to sort based on column say A or B. enter image description here

I followed the solution mentioned in the link but getting assertion failure - expected [ Array(3) ] to deeply equal [ Array(3) ]

function getCellTextAsArray(){
let cellContents = []
return new Cypress.Promise(resolve => {
    cy.get('#datatable-tabletools').find('tbody').children()

        .each(($el, $index) => {
            //some logic to select the elements you want
            //like $index % 4 == 0
            if($index>=0) {
                cellContents.push($el.text())
            }
        }).debug()
        .then(() => resolve(cellContents))
})
}

and then call this function as

getCellTextAsArray()
            .then(cellContents => {
                let actual = cellContents.slice()
                cy.wrap(actual)
                    .should('deep.eq', cellContents.sort())})   

Apologies, I am new to javascript.

Prany
  • 2,078
  • 2
  • 13
  • 31
  • The [`.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) function without a comparator, will compare using the default ascii sort. Try using `console.log(cellContents.sort())` to see if it was ordered as you expected. – Washington Guedes Aug 08 '19 at 14:23
  • @WashingtonGuedes - yes i tried and that's the problem, it is not sorted – Prany Aug 08 '19 at 14:56
  • Then you should add a comparator function for each data type, if you are trying to sort numbers you could do: `function compareNumbers (a, b) { return (+a) - (+b); }` and use `cellContents.sort(compareNumbers)`, for dates you may use some moment/luxon libs to parse the input correctly, for strings you can use the default ascii comparator. – Washington Guedes Aug 08 '19 at 16:35

1 Answers1

0
cy.get('#example>tbody > tr > :nth-child(1)') // this is the table column 'A'
    .then(function(A)
    {

        const age_values= A
        .toArray()
    
        .map($e1=> parseInt($e1.textContent))// to integer from constant
        
        expect(age_values).to.be.sorted({descending:true})//use chai-assertion

Pang
  • 9,564
  • 146
  • 81
  • 122