Bob*_*dje 28 javascript api alias cypress
是否可以cy.intercept在同一个测试中多次拦截同一个 API 调用?我尝试了以下方法:
cy.intercept({ pathname: "/url", method: "POST" }).as("call1")
// ... some logic
cy.wait("@call1")
// ... some logic
cy.intercept({ pathname: "/url", method: "POST" }).as("call2")
// ... some logic
cy.wait("@call2")
Run Code Online (Sandbox Code Playgroud)
我预计cy.wait("@call2")会等待第二次调用 API。但是,第二个cy.wait将立即继续,因为第一个 API 调用与第二个相同。
小智 29
更改日志显示,从此版本开始,拦截现在以相反的顺序调用
提供的响应处理程序(通过事件处理程序或通过 req.continue(cb) 提供)将以相反的顺序
cy.intercept()调用,直到调用 res.send 或直到不再有响应处理程序。
文档中的图表也对此进行了说明
当您设置相同的拦截时,第一个拦截将捕获所有呼叫。但您可以多次等待第一个别名。
这是一个(相当)简单的说明
规格
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')
})
Run Code Online (Sandbox Code Playgroud)
应用程序
<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>
Run Code Online (Sandbox Code Playgroud)
你可以在 Cypress 日志中看到它,
| 命令 | 称呼# | 出现(橙色标签) |
|---|---|---|
| 等待 | @call1 | 1 |
| 等待 | @call2 | |
| 等待 | @call1 | 2 |
// wait for 2 calls to complete
cy.wait('@queryGridInput').wait('@queryGridInput')
// get
cy.get("@queryGridInput.all").then((xhrs)=>{});
Run Code Online (Sandbox Code Playgroud)
@alias.all 将等待所有实例完成。它将返回一个 xhr 数组,其中包含所有匹配的 @alias。
https://www.cypress.io/blog/2019/12/23/asserting-network-calls-from-cypress-tests/
Cypress:我试图拦截源自 1 个按钮单击的 10 个调用,但 cy.wait().should 仅点击最后一个调用
小智 6
您有多种方法可以做到这一点:
为拦截的同一端点创建唯一的别名
cy.intercept({ pathname: "/posts", method: "POST" }).as("call")
//First Action
cy.get("button").click()
cy.wait("@call").its("request.url").should("contain", "somevalue")
//Second Action
cy.get("button2").click()
cy.wait("@call").its("request.url").should("contain", "othervalue")
Run Code Online (Sandbox Code Playgroud)
创建特定的en端点,可以使用glob模式生成动态端点
//notice path key instead of pathname
cy.intercept({path: "/post*parameter1=true", method: "POST"}).as("call1")
//First Action
cy.get("button").click()
cy.wait("@call1").its("request.url").should("contain", "somevalue")
cy.intercept({path: "/post*parameter2=false", method: "POST"}).as("call2")
//Second Action
cy.get("button2").click()
cy.wait("@call2").its("request.url").should("contain", "othervalue")
Run Code Online (Sandbox Code Playgroud)
验证结束时调用的所有端点
cy.intercept({ pathname: "/posts", method: "POST" }).as("call")
//First Action
cy.get("button").click()
cy.wait("@call")
//Second Action
cy.get("button2").click()
cy.wait("@call")
//you can add the number of request at the finish of alias
cy.get("@call.1").its("request.url").should("contain", "somevalue")
cy.get("@call.2").its("request.url").should("contain", "othervalue")
//another option instead of add the number of request maybe use the position in letters, but I think that it only works for first and last.
cy.get("@call.first").its("request.url").should("contain", "somevalue")
cy.get("@call.last").its("request.url").should("contain", "othervalue")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42936 次 |
| 最近记录: |