OR operator in Cypress.io

Viewed 7326

I am unable to build condition on which the test to be successful if page results are "No matching records found" OR "No data available". Is there an "OR" operator in Cypress? (none in the documentation)

I have tried to use nodejs to do the logic, but cypress throw error on the first condition.

        if (cy.get('.v-table__overflow > table > tbody > tr > td').contains('No matching records found') || cy.get('.v-table__overflow > table > tbody > tr > td').contains('No data available')) {
          cy.log('Record does not exist as Expected!');
        } else {
          cy.pause();
        }

enter image description here

2 Answers

Your Cypress code is totally incorrect. Please I suggest you to check how assertations work in Cypress.

UPDATE: You can achieve it doing something like this:

cy.get('.v-table__overflow > table > tbody > tr > td').shold('have.not.text', 'No matching records found').and('have.not.text', 'No data available');

or

cy.get('.v-table__overflow > table > tbody > tr > td').invoke('text').then((text) => {
  expect(text).not.equal('No matching records found')
  expect(text).not.equal('No data available')

})

Also, I see that you are checking that specific messages are not shown. Instead, consider to check that some elements are shown. This way test will be more clear.

You can use satisfy for this

cy.get('.v-table__overflow > table > tbody > tr > td')
    .should('satisfy', (elements) => {
        const text = elements[0].innerText;
        return text === 'No matching records found' || text === 'No data available';
    });
Related