Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

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

Cypress overwrite: I would like to overwrite the existing visit command 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

question from:https://stackoverflow.com/questions/65829580/cypress-how-do-i-overwrite-the-visit-command-to-always-attempt-to-dismiss-a-pop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One option could be instead of overwriting the cy.visit() command, you create a custom command with cy.visit(url) and then the code to close the popup.

1.Go to cypress/support/commands.js and write:

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

2.In your tests you can just write:

cy.openWebpage()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...