Sometimes elements on a webpage are hidden but still important to test. Handling hidden elements helps ensure your tests check everything correctly.
Hidden elements handling in Cypress
cy.get('selector', { timeout: 10000 }).should('not.be.visible') cy.get('selector', { force: true }).click()
Use { force: true } to interact with hidden elements.
Use assertions like .should('not.be.visible') to check if elements are hidden.
hiddenButton is hidden.cy.get('#hiddenButton').should('not.be.visible')
cy.get('#hiddenButton', { force: true }).click()
cy.get('.dropdown-menu').should('not.be.visible') cy.get('.dropdown-toggle').click() cy.get('.dropdown-menu').should('be.visible')
This test visits a page, checks a button is hidden, forces a click on it, then verifies it is visible after the click.
describe('Hidden Elements Handling', () => { beforeEach(() => { cy.visit('https://example.cypress.io/commands/actions') }) it('checks hidden element visibility and forces click', () => { // The button is hidden initially cy.get('.action-btn').should('not.be.visible') // Force click the hidden button cy.get('.action-btn', { force: true }).click() // After click, check if some visible change happened (example) cy.get('.action-btn').should('be.visible') }) })
Using { force: true } bypasses Cypress's default check for visibility before interacting.
Be careful when forcing actions on hidden elements; it may not reflect real user behavior.
Always verify if the element should be visible after interaction to ensure test reliability.
Hidden elements can be tested by checking visibility and forcing actions.
Use .should('not.be.visible') to assert hidden state.
Use { force: true } option to interact with hidden elements.
Practice
Solution
Step 1: Understand visibility assertion
The command.should('not.be.visible')checks if an element is hidden or not visible on the page.Step 2: Compare with other commands
Commands like.click()and.type()perform actions, not visibility checks..should('be.enabled')checks if an element is enabled, not visibility.Final Answer:
.should('not.be.visible') -> Option AQuick Check:
Check hidden =.should('not.be.visible')[OK]
- Using .click() to check visibility
- Confusing enabled state with visibility
- Using .type() for visibility check
Solution
Step 1: Identify force option usage
To click a hidden element, Cypress requires the option{ force: true }to override visibility checks.Step 2: Evaluate other options
Options like{ visible: true }or{ hidden: false }are invalid in Cypress click command. Clicking without force on hidden element fails.Final Answer:
cy.get('button.hidden').click({ force: true }) -> Option AQuick Check:
Click hidden = click with { force: true } [OK]
- Omitting { force: true } when clicking hidden elements
- Using invalid options like { visible: true }
- Trying to click without force on hidden elements
cy.get('#secret').should('not.be.visible')
cy.get('#secret').click()Assuming
#secret is hidden.Solution
Step 1: Check visibility assertion
The first line asserts the element is hidden, so it passes as expected.Step 2: Analyze click on hidden element
Clicking a hidden element without{ force: true }causes Cypress to fail the test because the element is not interactable.Final Answer:
Test fails at click because element is hidden -> Option DQuick Check:
Click hidden without force = fail [OK]
- Assuming click on hidden element passes
- Ignoring need for { force: true }
- Thinking click is silently ignored
cy.get('.hidden-link').click()But the test fails with an error about element not being visible. What is the best fix?
Solution
Step 1: Understand error cause
The error occurs because Cypress cannot click a hidden element without forcing the action.Step 2: Apply correct fix
Adding{ force: true }to.click()forces the click on the hidden element, fixing the error.Final Answer:
Add { force: true } to the click command -> Option CQuick Check:
Force click fixes hidden element error [OK]
- Trying to click without force on hidden elements
- Adding visibility assertion before click on hidden element
- Removing click instead of fixing it
#agree. Which Cypress code correctly checks it and verifies it is checked?Solution
Step 1: Identify hidden element interaction
Since the checkbox is hidden,.check()requires{ force: true }to interact with it.Step 2: Verify checked state
After forcing the check,.should('be.checked')confirms the checkbox is checked.Final Answer:
cy.get('#agree').check({ force: true }).should('be.checked') -> Option BQuick Check:
Check hidden checkbox with force and assert checked [OK]
- Using .check() without force on hidden checkbox
- Using .click() instead of .check() for checkbox
- Asserting not checked after clicking
Ready to prove it?
Pass the test and this pattern moves unknown
