QA Interview Hub · Knowledge base · Quizzes · Українська

Cypress › Outdated-answer traps

The element is hidden and the click doesn't go through. What do you do?

First find out why the element is hidden or covered instead of adding { force: true }, because that switches the checks off rather than fixing the problem. Before an action Cypress retries actionability checks until the timeout: the element is visible, enabled, uncovered and not animating. Remove the obstacle (a modal, a banner), wait for the state with should, and keep force for deliberately hidden elements, with a comment.

Cypress · Actions on elements

The click fails: the element is covered or hidden. Add force: true?

The test fails saying the element is hidden, covered by another one or disabled. The temptation is to add { force: true } and turn the test green. But that switches a check off rather than fixing anything. First understand why the element is hidden.

How it works

Before an action (click, type, check) Cypress runs actionability checks and retries them until the timeout: the element is visible, not disabled, not covered, not animating, scrolled into view. The error means the condition never held in the waiting time. The causes differ, and so do the fixes.

The cause decides the step

not there yetwait for a state: shouldcoveredclose the modal or bannertruly hiddena bug, or force with a comment
Figure 1. Diagnose before force.

Example: from force to the cause

// Before: a cookie banner covers it
cy.get('[data-cy=buy]').click({ force: true });
// After: remove the obstacle, wait for the state
cy.get('[data-cy=cookie-accept]').click();
cy.get('[data-cy=buy]').should('be.visible').click();

// a sticky header covers it after scrolling
cy.get('[data-cy=buy]').click({ scrollBehavior: 'center' });

The second version is more honest, because a user would also dismiss the banner first. If the banner genuinely gets in the way, that is the finding.

Limits and alternatives

force: true turns the actionability checks off: a test can pass where a person could not click. A justified use is deliberate: a hidden native checkbox under a custom one, an input type=file with no visible state. Then add a comment saying why. In Playwright it is the same: click({ force: true }) also skips the checks, and the same logic applies.

What exactly blocks an action

force: true turns off the actionability wait: Cypress fires events at once, even when the element is disabled or moving. The full list of checks is described on the "Interacting with Elements" page; in practice three places matter. Covering: a modal, an overlay, a sticky header. Animation: the element is still sliding into place, and Cypress waits until it stops. Element state: a button is disabled until form validation finishes, which is business logic the test has to wait for, not bypass.

// The button activates after validation: wait for state, not force
cy.get('[data-cy=email]').type('qa@example.com');
cy.get('[data-cy=submit]').should('be.enabled').click();

// A justified force: the native checkbox is hidden by styles
// (a custom toggle on top), so for the user it is "its own"
cy.get('input[type=checkbox]#agree').check({ force: true });
cy.get('#agree').should('be.checked'); // we verify the result

Note the technique: even when force is justified, an assertion on the result follows at once. That way the test does not pretend that the click had an effect. The scrollBehavior option takes an alignment ('center', 'nearest' and others) or false and fixes a typical problem: after scrolling the element ends up under a sticky header.

Common mistakes

  • force: true on every click: the suite no longer catches overlaps a user would see.
  • Not waiting for an element to appear and adding cy.wait: should('be.visible') is correct.
  • Ignoring animation: a click on a moving element misses, so wait for stability.

Check yourself

The error says div.modal covers the button. What do you check first?

Answer

Whether the modal should be on screen. If so, the test closes it; if not, it is an app bug, not a reason for force.

When is force: true justified?

Answer

When an element is deliberately hidden from the user but works (a native checkbox under a custom one), and a comment states the reason.

In the interview. Junior: First I find the cause: covered, not there yet, or a bug. I use force rarely and with a comment. Middle/Senior: force disables actionability, so a test can be green where a user would be stuck. So I remove the obstacle, wait for the state with should, and for a sticky header use scrollBehavior.

More articles in this chapter