skip to Main Content

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: "" },
{ id: 4, name: "skoda", saftyRating: "" }
]

When I iterate over the saftyRating field

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

It is failing because blank value it is converting 0 and 0 is not less than equal to -50

All I want is to append one more assert condition in OR form
Which should be like this .should('be.eq', 0);
So that that the test case passes

All any better approach to handle this is also welcome

2

Answers


  1. None of your safety ratings are less than -50, so .should('be.lt', -50) fails every data item.

    Change it to .should('be.lte', -50) which means "less than or equal".

    Login or Signup to reply.
  2. In my opinion, using +saftyRating is the wrong approach in this case, because the grid may contain a genuine 0 rating.

    An empty value such as saftyRating: "" is different to a zero value such as saftyRating: "0" – one is explicitly entered, the other is effectively "don’t know".

    To check two (or more) conditions, use a callback function something like

    data.forEach(({ saftyRating }) => {
      cy.wrap(saftyRating)
        .should(value => {
          expect(value === '' || +value <= -50).to eq(true)
        })
    })
    

    or with satisfy

    data.forEach(({ saftyRating }) => {
      cy.wrap(saftyRating)
        .should('satisfy', (value) => {
          return value === '' || +value <= -50
        })
    })
    

    You can easily extend the conditions if other requirements arise.

    More examples at this answer How to check that element has either of classes in Cypress

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search