简体   繁体   English

赛普拉斯仅拦截具有特定正文的消息

[英]Cypress intercept only message with specific body

I'm kind of new on Cypress intercept , but I am in need of capturing the response to a GET message, but wait for the message which has a response body with a specific value on its body.我是赛普拉斯intercept的新手,但我需要捕获对 GET 消息的响应,但等待消息的响应正文具有特定值。

For example, I may be receiving two responses to two GET requests, such as these two:例如,我可能会收到对两个 GET 请求的两个响应,例如这两个:

Event:                     request
cypress_runner.js:190995 Resource type:             xhr
cypress_runner.js:190995 Method:                    GET
cypress_runner.js:190995 Url:                       https://127.0.0.1/api/users/61a68c4a1d2c5258baece19c?_=1638304841558
cypress_runner.js:190995 Matched `cy.intercept()`:  {RouteMatcher: {…}, RouteHandler Type: 'Spy', RouteHandler: undefined, Request: {…}, Response: {…}, …}
cypress_runner.js:190995 Response status code:      200
cypress_runner.js:190995 Response headers:          {date: 'Tue, 30 Nov 2021 20:40:43 GMT', Content-Encoding: 'gzip', server: 'nginx', Vary: 'Accept-Encoding', access-control-allow-methods: 'PUT, GET, POST, DELETE, OPTIONS, PATCH', …}
cypress_runner.js:190995 Response body:             {"name": "Alice", "description": "Created by Cypress"}


Event:                     request
cypress_runner.js:190995 Resource type:             xhr
cypress_runner.js:190995 Method:                    GET
cypress_runner.js:190995 Url:                       https://127.0.0.1/api/users/61a68c4a1d2c5258baece19c?_=1638304841558
cypress_runner.js:190995 Matched `cy.intercept()`:  {RouteMatcher: {…}, RouteHandler Type: 'Spy', RouteHandler: undefined, Request: {…}, Response: {…}, …}
cypress_runner.js:190995 Response status code:      200
cypress_runner.js:190995 Response headers:          {date: 'Tue, 30 Nov 2021 20:40:43 GMT', Content-Encoding: 'gzip', server: 'nginx', Vary: 'Accept-Encoding', access-control-allow-methods: 'PUT, GET, POST, DELETE, OPTIONS, PATCH', …}
cypress_runner.js:190995 Response body:             {"name": "Bob", "description": "Created by Cypress"}

My intercept for now looks like this:我的拦截现在看起来像这样:

cy.intercept("GET", "/api/users/*").as("waitingForUpdateOnAlice")
cy.wait("@waitingForUpdateOnAlice")

But if the server returns the answer for Bob, then I don't have the chance of continue waiting.但是如果服务器返回 Bob 的答案,那么我就没有机会继续等待了。

Is there a way to handle this?有没有办法处理这个?

Mention: I do NOT have control or access to the trailing id on the url, so I do need to do this filtering only upon the response body.提及:我无法控制或访问 url 上的尾随 id,因此我只需要对响应正文进行此过滤。

You can alias an individual request .您可以为单个请求设置别名 This should accomplish what you'd like.这应该完成你想要的。

cy.intercept("GET", "/api/users/*", (req) => {
  if (req.body.name.equals('Alice')) {
    req.alias = 'waitingForUpdateOnAlice'
  }
});

cy.wait('@waitingForUpdateOnAlice');

You might need to play around with the exact form of that req.body.name conditional.您可能需要使用req.body.name条件的确切形式。

First of all, declare a function to be used recursively:首先,声明一个要递归使用的 function:

function doIntercept(functionToWait){
    cy.wait("@"+functionToWait).then((res)=>{
        if(res.response.body.name === 'Bob') {
            doIntercept("@"+functionToWait);
        } else {
            assert(res.response.body.name).to.be.eq('Alice');
        }
    })
}

then in your test section:然后在你的测试部分:

it("your test", ()=>{
    cy.intercept("GET", "/api/users/*").as("waitingForUpdateOnAlice");
    doIntercept("waitingForUpdateOnAlice");
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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