What if you could catch hidden bugs just by writing a few simple checks?
Why Common assertions (exist, be.visible, have.text) in Cypress? - Purpose & Use Cases
Imagine you have a website with many buttons and messages. You want to check if a button is there, if it can be seen, and if it shows the right words. Doing this by looking at the screen and clicking around takes a lot of time and you might miss things.
Manually checking each element is slow and tiring. You can easily forget to check some parts or make mistakes. Also, if the website changes, you have to check everything again by hand. This wastes time and can cause bugs to slip through.
Using common assertions like exist, be.visible, and have.text in Cypress lets you write simple commands that automatically check if elements are present, visible, and have the correct text. This makes testing fast, reliable, and repeatable.
if (document.querySelector('#submit')) { if (document.querySelector('#submit').offsetParent !== null) { if (document.querySelector('#submit').textContent === 'Send') { console.log('Button is ready') } } }
cy.get('#submit').should('exist').and('be.visible').and('have.text', 'Send')
It enables you to quickly and confidently check that your web page works as expected without missing important details.
For example, when testing a login page, you can automatically verify that the 'Login' button exists, is visible to users, and shows the correct label before trying to click it.
Manual checks are slow and error-prone.
Common assertions automate presence, visibility, and text checks.
This leads to faster, more reliable testing.
Practice
Solution
Step 1: Understand the meaning of
Theexistassertionexistassertion checks if the element is present in the DOM, even if it is hidden.Step 2: Compare with other assertions
be.visiblechecks if the element is visible,have.textchecks exact text content, andcontain.textchecks partial text. Onlyexistfits the question.Final Answer:
should('exist') -> Option DQuick Check:
Check presence = should('exist') [OK]
- Confusing 'exist' with 'be.visible'
- Using 'have.text' to check presence
- Thinking 'contain.text' checks existence
submitBtn is visible?Solution
Step 1: Identify the assertion for visibility
The assertionshould('be.visible')checks if the element is visible on the page.Step 2: Match syntax with element selector
cy.get('#submitBtn')selects the element by id. Combining withshould('be.visible')is correct syntax to check visibility.Final Answer:
cy.get('#submitBtn').should('be.visible') -> Option CQuick Check:
Visible check = should('be.visible') [OK]
- Using 'exist' instead of 'be.visible' for visibility
- Confusing 'have.text' with visibility check
- Using 'contain' without 'text' for exact text check
cy.get('.message').should('have.text', 'Welcome!')Assuming the element
<div class='message'>Welcome!</div> is present and visible.Solution
Step 1: Understand
This assertion checks if the element's text content exactly matches the given string.have.textassertionStep 2: Check element and text
The element<div class='message'>Welcome!</div>matches the text 'Welcome!' exactly, so the assertion passes.Final Answer:
Test passes because text matches exactly -> Option AQuick Check:
Exact text match = test passes [OK]
- Assuming visibility must be checked separately
- Thinking 'have.text' is invalid syntax
- Confusing 'have.text' with partial text check
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.Solution
Step 1: Check element visibility
The element hasstyle='display:none', so it is hidden and not visible.Step 2: Understand assertion failure
Theshould('be.visible')assertion will fail because the element is hidden, causing the test to fail before checking text.Final Answer:
The element is hidden, so 'be.visible' assertion fails -> Option BQuick Check:
Hidden element fails 'be.visible' [OK]
- Assuming 'have.text' runs even if visibility fails
- Thinking chaining assertions is invalid
- Ignoring element's CSS display property
.save-btn exists, is visible, and has the exact text Save. Which code snippet correctly performs all these checks?Solution
Step 1: Check presence, visibility, and exact text
We need to confirm the element exists, is visible, and has exact text 'Save'.Step 2: Verify chaining of assertions
cy.get('.save-btn').should('exist').should('be.visible').should('have.text', 'Save') chainsshould('exist'),should('be.visible'), andshould('have.text', 'Save')correctly to check all conditions.Final Answer:
cy.get('.save-btn').should('exist').should('be.visible').should('have.text', 'Save') -> Option AQuick Check:
Chain exist, visible, exact text for full check [OK]
- Skipping 'exist' check before visibility
- Using 'contain.text' instead of 'have.text' for exact match
- Not chaining all required assertions
Ready to prove it?
Pass the test and this pattern moves unknown
