mco*_*ool 6 cypress cypress-intercept
如果 Cypress 发出的请求没有与之关联的拦截,如何使 Cypress 测试失败?
假设 cypress 导航到一个页面并发出 10 个请求。如果这些请求中的任何一个没有被拦截,我怎样才能让 cypress 自动失败。我不希望赛普拉斯提出的任何请求不被嘲笑。
这可能吗?
如果要检查cy.intercept()应用程序请求的覆盖范围,请添加中间件拦截。
通常,您希望中间件捕获广泛的 URL,例如所有 API 调用都将被捕获
cy.intercept('**/api/**/*')
Run Code Online (Sandbox Code Playgroud)
中间件拦截始终首先查看捕获的调用,然后将它们传递给剩余的拦截。
此示例在加载期间检查 GET 调用https://cypress.io/。
cy.intercept('https://www.cypress.io/_astro/**/*') // the intercept in my test
// want to check for unhandled calls
// Middleware intercept - catches all calls
const calls = []
cy.intercept('https://www.cypress.io/**/*', { middleware: true }, (req) => {
calls.push(req.url)
}).as('all')
cy.visit('https://cypress.io/') // now visit the site
cy.then(() => {
// Use routes state info to compare handled calls vs all calls
const handledUrls = Object.values(cy.state('routes'))
.filter(route => route.alias !== 'all')
.map(route => Object.values(route.requests)
.map(interceptedRequest => interceptedRequest.request.url)
})
.flat()
// Find the unhandled calls
const unhandled = calls.filter(call => !handledUrls.includes(call))
console.log('Unhandled calls: ', unhandled)
})
Run Code Online (Sandbox Code Playgroud)
记录两个未处理的呼叫:
[
'https://www.cypress.io/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js',
'https://www.cypress.io/videos/home_page.webm'
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
440 次 |
| 最近记录: |