Home > Mobile >  How to do Conditionals in Cypress
How to do Conditionals in Cypress

Time:02-01

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?

CodePudding user response:

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.

CodePudding user response:

There is a whole section of the docs on Cypress.io for Conditional Testing.

  • Related