简体   繁体   中英

How can I exclude script tag from Cypress run?

I have a script tag that has to be active in production but can't be called when Cypress is running. As we run Cypress against our prod build as well, I can't use process.env.NODE_ENV to exclude it.

Is there any way to exclude a piece of code when Cypress is running?

As long as you can identify the the script exactly, you can modify the page source before it loads with cy.intercept()

cy.intercept(url, (req) => {
  req.continue((res) => {
    const scriptToRemove = '<script something-that-defines-the-script></script>'
    res.body = res.body.replace(scriptToRemove, '')
  })
}).as('page')

cy.visit(url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM