问题
I'm working on testing an OpenID Connect service, using Code and Implicit Flow. I would really like to be able to access the messages I get back from the service, especially the 303 See Other message which has the ID Token.
If someone can advise on how to get to response messages I would really appreciate it. Since the services exposes a HTML login page what happens is a cy.get("#loginButton").click() so I don't send a cy.request() and that is because I want to test login using the front-end.
回答1:
You should leverage cy.route, how it works:
- before
cy.visit
you need to addcy.server()
, it allows Cypress to intercept every request - you add an alias to the login request
cy.route({
method: "POST",
url: '/auth/token' // this is just an example, replace it with a part of the real URL called to log in the user
}).as("route_login"); // that's the alias, we'll use in soon
- right after the
cy.get("#loginButton").click()
command, you canwait
for the login request to happen
cy.wait("@route_login").then(xhr => {
// you can read the full response from `xhr.response.body`
cy.log(JSON.stringify(xhr.response.body));
});
your final test should be something like
it("Test description", () => {
cy.server();
cy.visit("YOUR_PAGE_URL");
cy.route({
method: "POST",
url: '/auth/token'
}).as("route_login");
cy.get("#loginButton").click();
cy.wait("@route_login").then(xhr => {
// you can read the full response from `xhr.response.body`
cy.log(JSON.stringify(xhr.response.body));
});
});
Let me know if you need more help 😉
来源:https://stackoverflow.com/questions/56005061/accessing-network-responses-in-cypress-io