Make {force:true} in click() function the default behavior
Asked Answered
W

2

5

I am using Cypress to test a web application. I am keep getting this visibility error (This element ''is not visible because its parent...) when trying to click links/buttons using the click() function.

Cypress' suggestion to 'Fix this problem, or use {force: true} to disable error checking' does help.

Now, I've been googling about how to make {force:true} the default behavior for the click() function so I do not have to write it in every use of the click() function but couldn't find anything so far - click({force:true}).

It that even possible? Any thoughts out there?

BR

Weintraub answered 20/4, 2020 at 11:55 Comment(0)
F
7

You could write a custom command for click called forceClick.

Cypress.Commands.add('forceClick', {prevSubject: 'element'}, (subject, options) => {
  cy.wrap(subject).click({force: true})
});

Then you can use:

cy.forceClick()

Rather than

cy.click()
Fogg answered 20/4, 2020 at 14:44 Comment(0)
P
4

You really should scrollIntoView() before you .click() in this case. I had a nasty UI bug where a button's request was being made 2x only in Cypress because we were force clicking a button not in view.

I'd suggest:

cy.get('button').scrollIntoView().click();
Purposive answered 1/12, 2020 at 23:48 Comment(1)
This is done by default when you call .click() : docs.cypress.io/guides/core-concepts/…Graphophone

© 2022 - 2024 — McMap. All rights reserved.