Bird
0
0

How can you assert that none of the list items li inside ul#menu have the class selected using Cypress negative assertions?

hard📝 Application Q9 of 15
Cypress - Assertions
How can you assert that none of the list items li inside ul#menu have the class selected using Cypress negative assertions?
Acy.get('ul#menu li').should('not.have.class', 'selected')
Bcy.get('ul#menu li').should('have.class', 'selected')
Ccy.get('ul#menu li').should('not.exist')
Dcy.get('ul#menu li').should('not.be.visible')
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    We want to check that no list item inside ul#menu has the class 'selected'.
  2. Step 2: Use negative class assertion on all items

    The command cy.get('ul#menu li').should('not.have.class', 'selected') asserts that none of the matched elements have the class 'selected'.
  3. Final Answer:

    cy.get('ul#menu li').should('not.have.class', 'selected') -> Option A
  4. Quick Check:

    Negative class assertion on multiple elements = B [OK]
Quick Trick: Use 'not.have.class' on collection to check none have class [OK]
Common Mistakes:
  • Using 'have.class' which checks for presence
  • Using 'not.exist' which checks for element absence
  • Checking visibility instead of class presence

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes