2

I have a scenario, where i need to put Assertion on an element's text which could be true OR pass the test case, if the any 1 value is present out of many.

Let say, an element can contain multiple status' : 'Open', 'Create', 'In Progress' any of these could be true.

How can i implement this scenario and Assert with OR logical operator or any other way?

cy.get('element').should('have.text', 'Open' | 'Create')

Amit Verma
  • 109
  • 7

3 Answers3

4

It sounds like a one-of assertion, something like the following:

cy.get('element')
  .invoke('text')
  .should('be.oneOf', ['Open', 'Create'])

To do it you need to extract the text before the assertion.

For reference see chaijs oneOf

Asserts that the target is a member of the given array list. However, it’s often best to assert that the target is equal to its expected value.

Woden
  • 168
  • 6
1

These both Worked:

cy.get('element')
  .invoke('text')
  .should('satisfy', (text) => text === 'option1' || text === 'option2')

OR

cy.get('element')
  .invoke('text')
  .should('be.oneOf', ['option1', 'option2']);
Amit Verma
  • 109
  • 7
-2

Wodens answer is most valuable if you wish to make an assertion and have the test fail if one of them doesn't exist

However, if you want to check the text of the element that is visible and do separate things based on which one is available:

cy
  .get('element')
  .filter(':visible')
  .invoke('text')
  .then((text) => {
       if(text.includes('Open')){
          // Things that only occur when Open is present
       } else if (text.includes('Create')){
          // Things that only occur when Create is present
       } else if (text.includes('In Progress')){
          // Things that only occur when In Progress is present
       } else {
         // Things that happen when the options are exhausted
       }
   });
KyleFairns
  • 2,947
  • 1
  • 15
  • 35
  • 1
    Thanks Kyle KyleFairns, At the moment i was just looking for putting an Assertion type. In your case, i might not be able to Pass/fail the Test Case on a specific option. – Amit Verma Jan 06 '23 at 10:40
  • @AmitVerma That's all good - I just wanted to provide an alternative solution for a case where you interact with the site in a slightly differently way with each of the use cases. This solution comes in handy when you need to hover over a row for more detail, or if there's a dropdown or modal that appears only when that text is present. If you were looking for a specific option, you could also use this method to check which additional steps are needed to guarantee a state – KyleFairns Jan 06 '23 at 11:24