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

Cypress › Locators and working with the DOM

Why is { force: true } dangerous?

{ force: true } skips actionability checks (visibility, covering, disabled) and fires the event at the element, bypassing the point on the screen. So a test passes where a real user cannot press the button, and it hides a bug. The cause is fixed first: remove the obstacle or wait for the state with an assertion. Force is rarely justified, for example for a native input hidden by design, and always with a comment.

Cypress · Locators and DOM

The click fails, force: true was added and the test is green. Did you fix the test or hide a bug?

A cookie banner covers the button, and cy.get(...).click() fails saying the element is covered by another one. The fastest “cure” is { force: true }: the test passes. Only a user on that page still cannot press the button.

How it works and why. Before click, type, check and other actions Cypress runs actionability: it scrolls the element into view and checks that it is visible, not disabled, not readonly, not covered, not animating and not detached from the DOM. All of this models a real user action. According to the docs, { force: true } “skips the checks and always fires the event at the desired element”: Cypress does not scroll, does not check visibility, covering, disabled and so on, and does not fire the event at a descendant. A real click goes to a point on the screen, while force bypasses the screen, so a covered element receives an event the user could not cause.

What force skips

Without forceexists, visible, not covered, enabledforce: truethe event goes straight to the element
Figure 1. Force removes exactly the protection the checks exist for.
Hides a bug

click({ force: true })

A banner covers the button, the test is green, the user is stuck. The test no longer checks what it should.

Checks

Remove the obstacle first

Close the banner or wait until it disappears, then click without force. If the banner cannot be closed, that is a bug.

Example: what to do instead of force.

// before: the test is green, the user cannot click
cy.get('[data-cy=buy]').click({ force: true });

// after: remove the real cause
cy.get('[data-cy=cookie-accept]').click();
cy.get('[data-cy=cookie-banner]').should('not.exist');
cy.get('[data-cy=buy]').should('be.enabled').click();

// justified, with a reason: the native input is hidden,
// a custom checkbox covers it
cy.get('input[type=checkbox]').check({ force: true });

The block with two assertions does not just wait: it describes the expected page state. Without force the test fails if the banner really cannot be closed, and that is a finding. For a hidden native input first try clicking its label or the custom element, as a user would.

Limits and alternatives. Justified cases are rare: native inputs hidden by design, elements that by design appear only on hover when hover cannot be reproduced. Before force try scrollIntoView, an assertion be.visible or be.enabled, the scrollBehavior setting, and for animations check waitForAnimations and animationDistanceThreshold. Next to every force leave a comment with the reason.

And in Playwright:

There is a similar { force: true } with the same risks, although per the docs it turns off only the “non-essential” actionability checks.

Common mistakes.

  • Adding force without finding the cause: the covering bug stays in the product and the suite cannot see it.
  • Putting force into a shared command (cy.clickForce): the hidden risk gets into every test.
  • Treating flakiness with force: flakiness is usually about animation or async loading, not about the checks.

Check yourself

A test with click({ force: true }) is green, yet users complain the button cannot be pressed. Why did the test not see it?

Answer

Force skips the visibility and covering checks and fires the event straight at the element, bypassing the point on the screen. A user presses what is on top, so the test and the user no longer perform the same action.

What do you do about a banner covering the button instead of force?

Answer

Close it with user actions and assert should('not.exist') or wait until it disappears, then click without force. If it cannot be closed, that is a product bug.

In an interview. Junior: Force turns off the checks: whether the element is visible, not covered, enabled. If a test passes only with force, it most likely hides a real bug. Middle/Senior: Force fires the event straight at the element while a user clicks a point on the screen, so the test stops modelling the user. I remove the cause first: close the obstacle, wait for the state with an assertion. I use force only for hidden native inputs, always with a comment.

More articles in this chapter