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
Step 1: Check element visibility
The element has style='display:none', so it is hidden and not visible.
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.
Final Answer:
The element is hidden, so 'be.visible' assertion fails -> Option B
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
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') chains should('exist'), should('be.visible'), and should('have.text', 'Save') correctly to check all conditions.
Final Answer:
cy.get('.save-btn').should('exist').should('be.visible').should('have.text', 'Save') -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.