0

I was wondering about this scenario:

I have a list of records, list items, and i want to do something on the items. The problem is, that i believe that when i query the items, sometimes I might have zero items, and other times I won't.

When i don't have any items, cy.get or cy.get(..).find(..) will timeout and the test will fail, but i do not want it to fail, but continue.

I have tried to chain cy.get/cy.get.find with each, but I do not think this would be possible.

  • 1
    I think you need to provide some more information. A bit of code, some html, your current code for the test.. – Maccurt Nov 27 '19 at 14:32

2 Answers2

0

What you need is an if in the code. It is not best practice to have a condition in your test, but sometimes that's the best it's gonna get.

To use an if clause you could use this syntax:

cy.get('body').then($body => {
  if ($body.find('<ELEMENT_YOUR_LOOKING_FOR').length === 1) {
    // action you want to perform on the list
  }
})

This way the test won't fail if there is no element found, it will simple not execute the if. But if the element is found, the actions will be executed.

Mr. J.
  • 3,499
  • 1
  • 11
  • 25
0

The simple answer is you should not have a test that may behave differently. The test should return the same result every time you run it.

So you need to make sure that when you run it your list will have some items and then you make some assertions on them. You can do it by preparing a test data on the test environment. And then you may have separate test when there is no items and make sure that it always will be so.

Misha Gariachiy
  • 103
  • 2
  • 7