最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cypress.io - Programmatically set response based on request parameters in cy.route() - Stack Overflow

programmeradmin3浏览0评论

I am stubbing an api request in my end-to-end test and would like to be able to return a different response depending on the request parameters that are sent out.

Currently, my stub is returning a static response regardless of what is posted, and looks like this:

cy.server()
cy.route({
    method: 'POST',
    url: '**/redeem-code',
    status: 200,
    response: {
        status: "Success"
    },
    delay: 500
})

I would like to be able to check the posted request parameters and then conditionally decide which response to return. I'm trying to do something like this:

cy.server()
cy.route({
    method: 'POST',
    url: '**/redeem-code',
    status: 200,
    response: (req) => {
        if(req.code == '1234') return { status: "Success" }
        else return { status: "Failure" }
    },
    delay: 500
})

Obviously, the code above doesn't work; it's just an example of what I'm trying to do.

I know Cypress allows for response methods, but I can't find the syntax for what I'm wanting to do anywhere in their docs. How do I get the request parameters in my response method so I can decide which response to return?

I am stubbing an api request in my end-to-end test and would like to be able to return a different response depending on the request parameters that are sent out.

Currently, my stub is returning a static response regardless of what is posted, and looks like this:

cy.server()
cy.route({
    method: 'POST',
    url: '**/redeem-code',
    status: 200,
    response: {
        status: "Success"
    },
    delay: 500
})

I would like to be able to check the posted request parameters and then conditionally decide which response to return. I'm trying to do something like this:

cy.server()
cy.route({
    method: 'POST',
    url: '**/redeem-code',
    status: 200,
    response: (req) => {
        if(req.code == '1234') return { status: "Success" }
        else return { status: "Failure" }
    },
    delay: 500
})

Obviously, the code above doesn't work; it's just an example of what I'm trying to do.

I know Cypress allows for response methods, but I can't find the syntax for what I'm wanting to do anywhere in their docs. How do I get the request parameters in my response method so I can decide which response to return?

Share Improve this question asked Aug 22, 2018 at 21:27 EmacsVIEmacsVI 5711 gold badge5 silver badges17 bronze badges 3
  • 1 EMacsVI, were you able to find a solution? I am facing the same problem – Digvijay Upadhyay Commented Dec 5, 2018 at 17:13
  • I am also looking for the solution to this. – Josh Bowden Commented Apr 23, 2019 at 16:55
  • almost there (github./cypress-io/cypress/pull/4176) – Daniel Commented Aug 3, 2020 at 21:41
Add a ment  | 

3 Answers 3

Reset to default 6

This is sadly currently not supported with cy.server.

The issue is being tracked here : https://github./cypress-io/cypress/issues/521

Workaround

Use standard javascript mocking. You can run this mocks in tests by using cypress onBeforeLoad, mentioned a few times in the linked issue. Its not pretty. Hopefully cypress gets native support in cy.server.

I had the same problem and made a feature to enable this. It does require a bounce back url to be stood up but the code install instructions can be seen here https://bitbucket/snippets/matt-tasc/onraxo

I believe this should work

cy.server({
        onResponse: ({ status, url, response }) => {
             if(url !== 'yoururl') return response;
             return { 
                ...response,
               body: { status: status === 1234 ? 'success' : 'failure' }
             };
        }    
});
发布评论

评论列表(0)

  1. 暂无评论