Cypress - Or Condition on Drop Down Button List

Viewed 462

I have a current Cypress check that iterates over a set of dropdowns checking for the case that the dropdown has 'Never eligible to update' as a value. However, I have to change the app so that one dropdown has 'Updates Sometimes'. I need to modify this test so that it passes if either of those two values is in the drop-down list. Not sure how to OR this.

cy.get("@updateRules")
  .find("button")
  .each(button => expect(button).to.contain("Never eligible to update"));
3 Answers

Possibly the simplest is regex

cy.get("@updateRules")
  .find("button")
  .each($button => {
    cy.wrap($button).contains(/Never eligible to update|Updates Sometimes/)
  })

or

cy.get("@updateRules")
  .find("button")
  .each($button => {
    expect($button).to.match(/Never eligible to update|Updates Sometimes/)
  })

Chai has a satisfy method

cy.get("@updateRules")
  .find("button")
  .each($button => expect($button.text()).to.satisfy(text => {
      return text.includes('Never eligible to update') || 
             text.includes('Updates Sometimes')
    })
  )

jQuery OR condition uses a comma between alternate values.

You will need to shift .contains() to :contains() inside the selector.

cy.get("@updateRules")
  .its('length')
  .then(ruleCount => {
    cy.get("@updateRules")
      .find("button:contains('Never eligible to update'), button:contains('Updates Sometimes')")
      .its('length')
      .then(matchCount => expect(ruleCount).to.eq(matchCount))
  })

You can also use a chai assertion .oneOf which asserts that the target is a member of the given array list.

cy.get("@updateRules")
  .find("button")
  .each($button => {
    expect($button.text().trim()).to.be.oneOf(["Never eligible to update", "Updates Sometimes"])
  })

You can OR match it using RegEx:

cy.get("@updateRules")
  .find("button")
  .each($button => {
    cy.wrap($button).contains(/Never eligible to update|Updates Sometimes/g)
  })
Related