0

As I am new to this UI automation/cypress world, need help to setting up the assertion on javascript object return by cypress-ag-grid package

My code is reading ag-grid data

cy.get("#myGrid").getAgGridData().should((data)=>{
cy.log(data)
})

Which is printing below object in console

[
{ id: 1, name: "tata", saftyRating: "50" },
{ id: 2, name: "maruti", saftyRating: "50" },
{ id: 3, name: "ford", saftyRating: "45" }
]

My cypress code is:

cy.get("#myGrid").getAgGridData().should((data)=>{
  data.forEach(({ saftyRating }) => {
    cy.wrap(+saftyRating).should('be.gt', 50);
  })
});

This is working fine till the moment I try make it generic parameterized typescript function which is as below:

columnValueAssertion(colname:string, asserttype:string,value:number){
cy.get("#myGrid").getAgGridData().should((data)=>{
      data.forEach(({ colname }) => {
        cy.wrap(+colname).should(asserttype, value);
      })
    });
}

And calling it as:

columnValueAssertion('saftyRating',"be.eq",50)

It is throwing assert error:

-assert NaN is not equal to **50**

And when I am replacing the colname by saftyRating it is working fine Not sure how convert this to parameterized function

1 Answers1

1

The problem is you're always trying to destructure a property named colname from the data, not a property with the name stored in colname. This results in undefined, and +undefined is NaN. You could use indexed access instead:

data.forEach((datum) => {
    cy.wrap(+datum[colname]).should(asserttype, value);
});

or you could rename the column with a computed property:

data.forEach(({ [colname]: column }) => {
    cy.wrap(+column).should(asserttype, value);
});
kelsny
  • 23,009
  • 3
  • 19
  • 48