2

I am writing a test in which if I land on a page and if any records are available, I need to click on three dots buttons near the record. But I should skip the test if no records are available on the page.

cy.get('body')
    .then(($body) => {

        if ($body.find('.ant-empty-description').length) {

            cy.log('Element not found. Skip the Test')

        }
        else {
            cy.xpath("//tbody[@class='ant-table-tbody']//tr[" + rowNumber + "]//td[4]//button//em").click()
        }
    })

I am using an approach in which if 'No Record found' message is present, I need to skip the test else click on the button present near the record.

Pablo
  • 310
  • 1
  • 6
  • A conditional test sounds like bad test writing. You should always have steps to set up your app for your test, ie your tests creates records and the records display in the table. – jjhelguero Jun 28 '22 at 15:43

2 Answers2

2

Sometimes it's necessary to test conditionally, but using <body> as a base element is a mistake IMO.

<body> is always on the page, but the table data may not be if fetched from an API.

The test will always run faster than the API, always see the empty row placeholder.

Pattern to use

Add an intercept() and wait for the API to respond.

Use the table rows as base element for conditional check - there will always be at least one row (but it may be the "No records" row).

cy.intercept(...).as('tableData')

...

cy.wait('@tableData')

cy.get('tbody tr').then($rows => {   // may be only one "No record" row

  const noData = $rows.text().includes('No Record found')

  if (!noData) {
    $rows.eq(rowNumber).find('td').eq(4]).find('button').click()
  }
})
Pablo
  • 310
  • 1
  • 6
0

You can use mocha's .skip() functionality. Please note that you'll have to use function() instead of arrow functions.

it('test', function() {
  cy.get('body')
    .then(function($body) {
      if ($body.find('.ant-empty-description').length) {
        cy.log('Element not found. Skip the Test')
        this.skip()
      } else {
        cy.xpath("//tbody[@class='ant-table-tbody']//tr[" + rowNumber + "]//td[4]//button//em").click()
      }
    })
})

That being said, I agree with @jjhelguero -- using skips in this way is an anti-pattern for testing. Ideally, you should control whether or not an element will appear on the webpage, and use your test setup to manipulate the page into having/not having the element.

agoff
  • 5,818
  • 1
  • 7
  • 20
  • I tried above code but I am getting below error from Cypress. Timed out retrying after 30000ms: Expected to find element: //tbody[@class='ant-table-tbody']//tr[1]//td[4]//button//em, but never found it. Not sure why it is searching for the element , even if I am putting it in else condition Also. instead of using describe it format, I am using Cucumber (cypress-cucumber-preprocessor) – Bhawin Joshi Jun 29 '22 at 12:11
  • Have you tried logging the value for `$body.find('.ant-empty-description').length`? Is it possible that the value is > 0 even when you aren't seeing the element? – agoff Jun 29 '22 at 14:07
  • As for using cucumber, I have no experience with that and can't provide any guidance on using this strategy with cucumber. – agoff Jun 29 '22 at 14:07
  • I tried logging the value and I am getting it as '0' even if the element is present on the page. Ideally I should get it as '1' and the test should be skipped – Bhawin Joshi Jul 06 '22 at 09:10