What if you could click invisible buttons without lifting a finger?
Why Force option for hidden elements in Cypress? - Purpose & Use Cases
Imagine you are testing a website where some buttons or inputs are hidden behind menus or overlays. You try clicking them manually, but since they are not visible, your clicks don't work. You have to open menus or scroll carefully every time to reach them.
Manually testing hidden elements is slow and frustrating. You might miss clicking some hidden buttons or enter wrong data because the element is not visible. It's easy to make mistakes or waste time trying to find and interact with these hidden parts.
The force option in Cypress lets you interact with hidden elements directly. It tells Cypress to click or type on elements even if they are not visible on the screen. This saves time and avoids errors by skipping the need to reveal elements first.
cy.get('#submit').click(); // Fails if button is hidden
cy.get('#submit').click({ force: true }); // Clicks even if hidden
Using the force option enables smooth, reliable testing of all elements, visible or hidden, without extra steps.
Testing a dropdown menu where the submit button is hidden until you open the menu. Instead of opening the menu manually, you use force to click the submit button directly.
Manual testing hidden elements is slow and error-prone.
Force option lets tests interact with hidden elements easily.
This makes tests faster and more reliable.
Practice
force: true option do in Cypress when interacting with elements?Solution
Step 1: Understand the purpose of
This option tells Cypress to ignore visibility checks and interact with elements even if they are hidden.force: trueStep 2: Compare with default behavior
By default, Cypress only interacts with visible elements. Usingforce: trueoverrides this.Final Answer:
Allows Cypress to interact with elements even if they are hidden or not visible. -> Option AQuick Check:
force: true= interact hidden elements [OK]
- Thinking force waits for visibility
- Assuming force disables tests on hidden elements
- Confusing force with automatic retries
submitBtn using Cypress?Solution
Step 1: Recall correct object syntax for options
Cypress expects options as an object inside curly braces with key-value pairs, e.g.,{ force: true }.Step 2: Identify correct syntax among options
cy.get('#submitBtn').click({ force: true }); uses the correct object syntax with curly braces and colon separator.Final Answer:
cy.get('#submitBtn').click({ force: true }); -> Option DQuick Check:
Options object uses curly braces and colon [OK]
- Using parentheses instead of braces for options
- Using equal sign instead of colon
- Missing braces around options
cy.get('.hidden-checkbox').check({ force: true });
cy.get('.hidden-checkbox').should('be.checked');What will be the result of this test?
Solution
Step 1: Understand the effect of
Usingforce: trueon hidden checkboxesforce: trueallows Cypress to check the checkbox even if it is hidden.Step 2: Verify the assertion outcome
The next line asserts the checkbox is checked, which will be true because the previous command succeeded.Final Answer:
The test will pass because force allows checking hidden checkboxes. -> Option BQuick Check:
force: true lets hidden checkbox be checked [OK]
- Assuming hidden elements cannot be checked
- Confusing force with visibility wait
- Expecting syntax errors from correct code
cy.get('#hiddenBtn').click();It fails because the button is hidden. How can you fix it without changing the HTML?
Solution
Step 1: Identify why the test fails
The button is hidden, so Cypress refuses to click it by default.Step 2: Apply the force option to override visibility check
Adding{ force: true }to the click command forces Cypress to click the hidden button.Final Answer:
Add { force: true } to the click command. -> Option CQuick Check:
Force option overrides hidden element click failure [OK]
- Adding visibility assertion before click (won't fix hidden)
- Using invalid option like wait: true
- Changing selectors instead of fixing interaction
Solution
Step 1: Understand dropdown visibility and interaction
The dropdown is hidden by default, so normalselectwon't work without visibility.Step 2: Use force option to select hidden option
Using{ force: true }allows Cypress to select the option even if the dropdown is hidden.Step 3: Evaluate other options
cy.get('#dropdown').select('Option 1'); fails due to hidden dropdown. cy.get('#dropdown').invoke('show').select('Option 1'); tries to show dropdown via invoke but may not trigger UI properly. cy.get('#dropdown').click().select('Option 1'); clicks dropdown but does not guarantee visibility or selection.Final Answer:
cy.get('#dropdown').select('Option 1', { force: true }); -> Option AQuick Check:
Select hidden dropdown option using force: true [OK]
- Trying to select without force on hidden dropdown
- Using invoke('show') without proper UI event
- Clicking dropdown without ensuring visibility
Ready to prove it?
Pass the test and this pattern moves unknown
