在 Cypress.io 中访问网络响应

use*_*786 1 oauth single-sign-on cypress

我正在使用代码和隐式流测试 OpenID Connect 服务。我真的希望能够访问我从服务返回的消息,尤其是具有 ID 令牌的 303 See Other 消息。

如果有人可以就如何获取回复消息提供建议,我将不胜感激。由于服务公开了一个 HTML 登录页面,所以发生的是 cy.get("#loginButton").click() 所以我不发送 cy.request() 这是因为我想使用前端测试登录 -结尾。

Nor*_*Ste 7

您应该利用cy.route,它是如何工作的:

  • cy.visit您需要添加之前cy.server(),它允许 Cypress 拦截每个请求
  • 您为登录请求添加别名
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
Run Code Online (Sandbox Code Playgroud)
  • cy.get("#loginButton").click()命令之后,您可以wait让登录请求发生
cy.wait("@route_login").then(xhr => {
  // you can read the full response from `xhr.response.body`
  cy.log(JSON.stringify(xhr.response.body));
});
Run Code Online (Sandbox Code Playgroud)

你的最终测试应该是这样的

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));
  });

});
Run Code Online (Sandbox Code Playgroud)

如果您需要更多帮助,请告诉我