Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 376
shayyo Avatar asked Sep 04 '26 23:09

shayyo


2 Answers

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()
like image 97
mvoase Avatar answered Sep 06 '26 13:09

mvoase


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();
like image 22
Fillip Peyton Avatar answered Sep 06 '26 12:09

Fillip Peyton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!