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

Cypress overwrite: I would like to overwrite the existing visit mand so that it still operates as is, b

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745673847a4639565.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信