javascript - Cypress: How do I overwrite the visit command to always attempt to dismiss a popup after the page is loaded? - Stac

admin2025-04-26  5

Cypress overwrite: I would like to overwrite the existing visit mand so that it still operates as is, but will attempt to dismiss a popup after the visit has successfully executed.

The popup is something we have very little control over and it appears after you login. Seeing as we're bypassing the login screen and logging in programmatically, we'll see the popup when we navigate to any page. The insufficient code I currently have:

Cypress.Commands.overwrite('visit', (originalFn, url, options) => {
originalFn(url, options);

cy.get("body").then($body => {
  if ($body.find("[text='Got it']").length > 0) { 
     cy.contains("Got it", { matchCase: false }).click();
  }
});
});

Thanks

Cypress overwrite: I would like to overwrite the existing visit mand so that it still operates as is, but will attempt to dismiss a popup after the visit has successfully executed.

The popup is something we have very little control over and it appears after you login. Seeing as we're bypassing the login screen and logging in programmatically, we'll see the popup when we navigate to any page. The insufficient code I currently have:

Cypress.Commands.overwrite('visit', (originalFn, url, options) => {
originalFn(url, options);

cy.get("body").then($body => {
  if ($body.find("[text='Got it']").length > 0) { 
     cy.contains("Got it", { matchCase: false }).click();
  }
});
});

Thanks

Share Improve this question edited Jan 21, 2021 at 14:34 mckenzit asked Jan 21, 2021 at 14:29 mckenzitmckenzit 531 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You can do this by overwriting cy.visit() mand. Try this:

Cypress.Commands.overwrite('visit', (originalFn, url, options) => {
  originalFn(url, options);
  // make sure to add a return here!
  return cy.get('body').then($body => {
    if ($body.find("[text='Got it']").length > 0) {
      cy.contains('Got it', { matchCase: false }).click();
    }
  });
});

source: https://docs.cypress.io/api/cypress-api/custom-mands#Overwrite-visit-mand

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745673847a313472.html

最新回复(0)