更新为 Cypress v7.0.0 于 2021 年 4 月 5 日发布
更改日志显示,从此版本开始,拦截现在以相反的顺序调用
响应处理程序(通过事件处理程序或通过 req.continue(cb) 提供)提供给cy.intercept()
将被叫入相反的顺序直到调用 res.send 或直到不再有响应处理程序。
文档中的图表也对此进行了说明
当您设置相同的拦截时,第一个拦截将捕获所有呼叫。但您可以多次等待第一个别名。
这是一个(相当)简单的说明
spec
Cypress.config('defaultCommandTimeout', 10); // low timeout
// makes the gets depend on the waits
// for success
it('never fires @call2',()=>{
cy.intercept({ pathname: "/posts", method: "POST" }).as("call1")
cy.intercept({ pathname: "/posts", method: "POST" }).as("call2")
cy.visit('../app/intercept-identical.html')
cy.wait('@call1') // call1 fires
cy.get('div#1').should('have.text', '201')
cy.wait('@call2') // call2 never fires
cy.wait('@call1') // call1 fires a second time
cy.get('div#2').should('have.text', '201')
})
app
<body>
<div id="1"></div>
<div id="2"></div>
<script>
setTimeout(() => {
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({ title: 'foo', body: 'bar', userId: 1 }),
headers: { 'Content-type': 'application/json; charset=UTF-8' },
}).then(response => {
document.getElementById('1').innerText = response.status;
})
}, 500)
setTimeout(() => {
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({ title: 'foo', body: 'bar', userId: 2 }),
headers: { 'Content-type': 'application/json; charset=UTF-8' },
}).then(response => {
document.getElementById('2').innerText = response.status;
})
}, 1000)
</script>
</body>
你可以在 Cypress 日志中看到它,
command |
call# |
occurrence (orange tag) |
wait |
@call1 |
1 |
wait |
@call2 |
|
wait |
@call1 |
2 |