admin管理员组文章数量:1391925
I want to stub the same API endpoint twice, so the second call returns different response than the first one. Here is snippet how I imagine this would work:
cy.route('POST', 'access-tokens', '@loginFailResponse', {status: 401}).as('loginFail')
cy.route('POST', 'access-tokens', '@loginSuccessResponse').as('loginSuccess')
The first time I try to login it denies access, I change form inputs and then it should let me in.
I tried to wrap second cy.route(...)
definition as callback function to first output but cypress denies calling cy.anything
in promises. Like in example below
cy.route('POST', 'access-tokens', '@loginFailResponse', {status: 401, onResponse: () => {
cy.fixture('login_screen/login_success_response.json').as('loginSuccessResponse')
cy.route('POST', 'access-tokens', '@loginSuccessResponse').as('loginSuccess')}
}}).as('loginFail')
here is my test case:
cy.route('POST', 'access-tokens', '@loginFailResponse', {status: 401}).as('loginFail')
cy.route('POST', 'access-tokens', '@loginSuccessResponse').as('loginSuccess')
cy.get("form input[type='email']").type("[email protected]")
cy.get("form input[type='password']").type("Bad password")
// this should fail
cy.get("form").submit()
cy.get("form input[type='password']").type("[email protected]")
// this should let me in
cy.get("form").submit()
I want to stub the same API endpoint twice, so the second call returns different response than the first one. Here is snippet how I imagine this would work:
cy.route('POST', 'access-tokens', '@loginFailResponse', {status: 401}).as('loginFail')
cy.route('POST', 'access-tokens', '@loginSuccessResponse').as('loginSuccess')
The first time I try to login it denies access, I change form inputs and then it should let me in.
I tried to wrap second cy.route(...)
definition as callback function to first output but cypress denies calling cy.anything
in promises. Like in example below
cy.route('POST', 'access-tokens', '@loginFailResponse', {status: 401, onResponse: () => {
cy.fixture('login_screen/login_success_response.json').as('loginSuccessResponse')
cy.route('POST', 'access-tokens', '@loginSuccessResponse').as('loginSuccess')}
}}).as('loginFail')
here is my test case:
cy.route('POST', 'access-tokens', '@loginFailResponse', {status: 401}).as('loginFail')
cy.route('POST', 'access-tokens', '@loginSuccessResponse').as('loginSuccess')
cy.get("form input[type='email']").type("[email protected]")
cy.get("form input[type='password']").type("Bad password")
// this should fail
cy.get("form").submit()
cy.get("form input[type='password']").type("[email protected]")
// this should let me in
cy.get("form").submit()
Share
Improve this question
asked May 9, 2018 at 10:08
Filip BartuziFilip Bartuzi
5,9318 gold badges58 silver badges105 bronze badges
1 Answer
Reset to default 7Everytime you define .route('VERB', '/endpoint', ...)
it overrides your previous definition. The simplest solution would be to override this endpoint after you finish your first call
This test will work for you, Filip.
cy.route('POST', 'access-tokens', '@loginFailResponse', {status: 401}).as('loginFail')
cy.get("form input[type='email']").type("[email protected]")
cy.get("form input[type='password']").type("Bad password")
// this should fail
cy.get("form").submit()
cy.route('POST', 'access-tokens', '@loginSuccessResponse').as('loginSuccess')
cy.get("form input[type='password']").type("[email protected]")
// this should let me in
cy.get("form").submit()
本文标签: javascriptStub the same endpoint twice in cypressStack Overflow
版权声明:本文标题:javascript - Stub the same endpoint twice in cypress - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744662420a2618324.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论