在Cypress.io中访问网络响应

时间:2019-05-06 12:17:45

标签: oauth single-sign-on cypress

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

如果有人可以建议如何回复邮件,我将非常感谢。由于服务公开了HTML登录页面,因此发生了 cy.get(“#loginButton”)。click() 所以我不发送cy.request(),这是因为我想使用前端测试登录。

2 个答案:

答案 0 :(得分:1)

在赛普拉斯6.x中不推荐使用

@AndroidEntryPointcy.server()


改为使用cy.route()

cy.intercept()

您的测试可以拦截,修改并等待源自您应用的任何类型的HTTP请求。

文档:https://docs.cypress.io/api/commands/intercept.html(带有示例)

答案 1 :(得分:0)

您应该利用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
  • cy.get("#loginButton").click()命令之后,您可以wait进行登录请求
cy.wait("@route_login").then(xhr => {
  // you can read the full response from `xhr.response.body`
  cy.log(JSON.stringity(xhr.response.body));
});

您的最终测试应该类似于

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.stringity(xhr.response.body));
  });

});

让我知道您是否需要更多帮助