Accessing network responses in Cypress.io

早过忘川 提交于 2020-03-23 12:21:09

问题


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 add cy.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 can wait 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!