javascriptcypresse2e-testingcypress-conditional-testing

How to do Conditionals in Cypress


In my e2e, I need to check if the datatable is populated first before before checkboxes in the table are clicked.

I am able to check the count like so

cy.get('.p-datatable-table').find('tr').its('length').should('be.gte', 0);

unfortunately, the below does not work.

  if (cy.get('.p-datatable-table').find('tr').its('length').should('be.gte', 0)) {
    cy.get('.select-all-boxes').click();
  }

Any suggestions?


Solution

  • You can't use the expression cy.get('.p-datatable-table').find('tr').its('length').should('be.gte', 0) to perform an if check.

    The result of that expression is a chainable, so you have to chain it

    cy.get('.p-datatable-table').find('tr').its('length')
      .then(length => {
        if ( length ) {
          cy.get('.select-all-boxes').click()
        }
      })
    

    Not sure what you expect with .should('be.gte', 0) but it does nothing so I dropped it.