Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 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 Answer

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...