Bird
Raised Fist0
Cypresstesting~20 mins

Common assertions (exist, be.visible, have.text) in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Assertion Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate
2:00remaining
Check element visibility and text content
You want to verify that a button with id #submitBtn is visible and has the exact text Submit. Which Cypress assertion chain correctly does this?
Cypress
cy.get('#submitBtn')
Ashould('exist').and('be.visible').and('have.text', 'Submit')
Bshould('be.visible').and('exist').and('contain.text', 'Submit')
Cshould('exist').and('have.text', 'Submit').and('be.visible')
Dshould('be.visible').and('have.text', 'Submit').and('exist')
Attempts:
2 left
💡 Hint
Remember the order of assertions does not affect the test, but the exact text match requires 'have.text'.
❓ assertion
intermediate
1:30remaining
Detect missing element with assertion
You want to confirm that an element with class .error-message does NOT exist on the page. Which assertion correctly verifies this?
Cypress
cy.get('.error-message')
Ashould('have.length', 0)
Bshould('not.be.visible')
Cshould('not.have.text')
Dshould('not.exist')
Attempts:
2 left
💡 Hint
To check absence, use the 'not.exist' assertion.
❓ Predict Output
advanced
2:00remaining
What is the test result of this assertion chain?
Given the HTML <div id='msg' style='display:none'>Hello</div>, what will be the result of this Cypress code?
Cypress
cy.get('#msg').should('exist').and('be.visible').and('have.text', 'Hello')
ATest fails at 'be.visible' assertion
BTest passes successfully
CTest fails at 'have.text' assertion
DTest fails at 'exist' assertion
Attempts:
2 left
💡 Hint
The element exists but is hidden by CSS.
❓ locator
advanced
1:30remaining
Choose the best locator for asserting text content
You want to assert the text of a paragraph inside a section with id #info. Which locator is best to use with Cypress for this assertion?
Acy.get('.info p')
Bcy.get('#info > p')
Ccy.contains('p', 'expected text')
Dcy.get('p').eq(0)
Attempts:
2 left
💡 Hint
Use a locator that is specific and stable.
❓ framework
expert
2:00remaining
Identify the failing assertion in a chained test
Consider this Cypress test code snippet:
cy.get('.notification')
  .should('exist')
  .and('be.visible')
  .and('have.text', 'Success')
  .and('have.class', 'active')

If the element exists, is visible, has text 'Success', but does NOT have class 'active', which assertion causes the test to fail?
Cypress
cy.get('.notification').should('exist').and('be.visible').and('have.text', 'Success').and('have.class', 'active')
A'have.text' assertion fails
B'exist' assertion fails
C'have.class' assertion fails
D'be.visible' assertion fails
Attempts:
2 left
💡 Hint
Check which condition is not met by the element.

Practice

(1/5)
1. Which Cypress assertion checks if an element is present in the DOM, regardless of visibility?
easy
A. should('contain.text')
B. should('be.visible')
C. should('have.text')
D. should('exist')

Solution

  1. Step 1: Understand the meaning of exist assertion

    The exist assertion checks if the element is present in the DOM, even if it is hidden.
  2. Step 2: Compare with other assertions

    be.visible checks if the element is visible, have.text checks exact text content, and contain.text checks partial text. Only exist fits the question.
  3. Final Answer:

    should('exist') -> Option D
  4. Quick Check:

    Check presence = should('exist') [OK]
Hint: Exist means element is in DOM, visible or not [OK]
Common Mistakes:
  • Confusing 'exist' with 'be.visible'
  • Using 'have.text' to check presence
  • Thinking 'contain.text' checks existence
2. Which of the following is the correct Cypress syntax to assert that an element with id submitBtn is visible?
easy
A. cy.get('#submitBtn').should('exist')
B. cy.get('#submitBtn').should('contain', 'Submit')
C. cy.get('#submitBtn').should('be.visible')
D. cy.get('#submitBtn').should('have.text', 'Submit')

Solution

  1. Step 1: Identify the assertion for visibility

    The assertion should('be.visible') checks if the element is visible on the page.
  2. Step 2: Match syntax with element selector

    cy.get('#submitBtn') selects the element by id. Combining with should('be.visible') is correct syntax to check visibility.
  3. Final Answer:

    cy.get('#submitBtn').should('be.visible') -> Option C
  4. Quick Check:

    Visible check = should('be.visible') [OK]
Hint: Use should('be.visible') to check if element shows on page [OK]
Common Mistakes:
  • Using 'exist' instead of 'be.visible' for visibility
  • Confusing 'have.text' with visibility check
  • Using 'contain' without 'text' for exact text check
3. What will be the result of this Cypress test code?
cy.get('.message').should('have.text', 'Welcome!')

Assuming the element <div class='message'>Welcome!</div> is present and visible.
medium
A. Test passes because text matches exactly
B. Test fails because element is not checked for visibility
C. Test fails because 'have.text' is incorrect syntax
D. Test passes but ignores text content

Solution

  1. Step 1: Understand have.text assertion

    This assertion checks if the element's text content exactly matches the given string.
  2. Step 2: Check element and text

    The element <div class='message'>Welcome!</div> matches the text 'Welcome!' exactly, so the assertion passes.
  3. Final Answer:

    Test passes because text matches exactly -> Option A
  4. Quick Check:

    Exact text match = test passes [OK]
Hint: Exact text match means test passes with have.text [OK]
Common Mistakes:
  • Assuming visibility must be checked separately
  • Thinking 'have.text' is invalid syntax
  • Confusing 'have.text' with partial text check
4. Identify the error in this Cypress test code:
cy.get('.alert').should('be.visible').should('have.text', 'Error occurred')

Given the element <div class='alert' style='display:none'>Error occurred</div> is hidden.
medium
A. The element selector is incorrect
B. The element is hidden, so 'be.visible' assertion fails
C. 'have.text' assertion syntax is wrong
D. Chaining assertions is not allowed in Cypress

Solution

  1. Step 1: Check element visibility

    The element has style='display:none', so it is hidden and not visible.
  2. Step 2: Understand assertion failure

    The should('be.visible') assertion will fail because the element is hidden, causing the test to fail before checking text.
  3. Final Answer:

    The element is hidden, so 'be.visible' assertion fails -> Option B
  4. Quick Check:

    Hidden element fails 'be.visible' [OK]
Hint: Hidden elements fail 'be.visible' assertion [OK]
Common Mistakes:
  • Assuming 'have.text' runs even if visibility fails
  • Thinking chaining assertions is invalid
  • Ignoring element's CSS display property
5. You want to write a Cypress test to confirm a button with class .save-btn exists, is visible, and has the exact text Save. Which code snippet correctly performs all these checks?
hard
A. cy.get('.save-btn').should('exist').should('be.visible').should('have.text', 'Save')
B. cy.get('.save-btn').should('be.visible').should('contain.text', 'Save')
C. cy.get('.save-btn').should('exist').should('have.text', 'Save')
D. cy.get('.save-btn').should('have.text', 'Save').should('be.visible')

Solution

  1. Step 1: Check presence, visibility, and exact text

    We need to confirm the element exists, is visible, and has exact text 'Save'.
  2. Step 2: Verify chaining of assertions

    cy.get('.save-btn').should('exist').should('be.visible').should('have.text', 'Save') chains should('exist'), should('be.visible'), and should('have.text', 'Save') correctly to check all conditions.
  3. Final Answer:

    cy.get('.save-btn').should('exist').should('be.visible').should('have.text', 'Save') -> Option A
  4. Quick Check:

    Chain exist, visible, exact text for full check [OK]
Hint: Chain all assertions to check presence, visibility, and text [OK]
Common Mistakes:
  • Skipping 'exist' check before visibility
  • Using 'contain.text' instead of 'have.text' for exact match
  • Not chaining all required assertions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard