0
0
Cypresstesting~10 mins

Force option for hidden elements in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks clicking a hidden button using Cypress's force: true option. It verifies that the click works even if the button is not visible.

Test Code - Cypress
Cypress
describe('Force click on hidden element', () => {
  it('Clicks hidden button using force option', () => {
    cy.visit('https://example.cypress.io/commands/actions')
    // The button is hidden by default
    cy.get('#hidden-button').click({ force: true })
    // Verify the click triggered a visible change
    cy.get('#hidden-button-clicked').should('be.visible')
  })
})
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initialized, browser ready-PASS
2Browser opens and navigates to https://example.cypress.io/commands/actionsPage loaded with elements including a hidden button with id 'hidden-button'-PASS
3Find element with id 'hidden-button'Hidden button element located in DOM but not visibleElement exists in DOMPASS
4Click the hidden button using { force: true } optionButton is clicked despite being hiddenClick event triggered on hidden buttonPASS
5Check that element with id 'hidden-button-clicked' is visibleElement '#hidden-button-clicked' is visible on page indicating click successElement '#hidden-button-clicked' is visiblePASS
Failure Scenario
Failing Condition: Click fails because force option is not used on hidden element
Execution Trace Quiz - 3 Questions
Test your understanding
Why is the { force: true } option used in cy.click()?
ATo scroll the element into view before clicking
BTo click an element even if it is hidden or covered
CTo wait for the element to become visible before clicking
DTo disable clicking on the element
Key Result
Use the { force: true } option in Cypress to interact with elements that are hidden or covered, ensuring tests can simulate user actions even when elements are not visible.