Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i compare arrays in Cypress?

Tags:

cypress

I want to compare data present in an array with the data retrieved from a class using .each in cypress?

Using below code i have tried to iterate over the FileType array using below code.

const Filetype = ['Access', 'Excel', 'Text/CSV','PDF','JSON','dsdsd'];
const te = cy.wrap($bodyFind).find('.data-sourcename').should('have.length',6).each(($li) => { cy.log($li.text()); });

te.each(($text)=> {
cy.log("Te" + $text); 
//prints ['Access','Excel','Text/CSV','PDF','JSON','XML'];
});

// Converted FileType Array to Cypress object using cy.wrap command.
const cywrap = cy.wrap(Filetype);
      te.each((e1)=>cywrap.each((e2)=> {
      if(e1 == e2) {
         expect(e1).equals(e2);
       }
     }));

But the value of e1 and e2 is same. expect should fail with 'dsdsd' is equals 'XML' whereas it passes with 'dsdsd' is equals 'dsdsd'

like image 349
Nikkie08 Avatar asked Sep 26 '19 05:09

Nikkie08


2 Answers

You could use map here.

const filetypes = ['Access', 'Excel', 'Text/CSV','PDF','JSON','dsdsd'];
cy.get('.data-sourcename').should(($els) => {
  // map jquery elements to array of their innerText
  const elsText = $els.toArray().map(el => el.innerText)
  expect(elsText).to.deep.eq(filetypes)
})
like image 141
bkucera Avatar answered Sep 28 '22 01:09

bkucera


I hope by now you must have found out the solution for this. But still, there was no selected answers yet, thought I would add one.

const Filetype = ['Access', 'Excel', 'Text/CSV','PDF','JSON','dsdsd'];

cy
   .get('whatever element')
   .each(($span, i) => {
        expect($span.text()).to.equal(Filetype[i]);
   });

$span would go through each element and .text()will get the text value of that element.

like image 39
Debu Shinobi Avatar answered Sep 28 '22 01:09

Debu Shinobi