【问题标题】:Cypress - test redirect赛普拉斯 - 测试重定向
【发布时间】:2021-10-14 20:23:48
【问题描述】:

我有一个本地端点来检查我是否已登录,如果是,它会生成令牌并将我重定向到另一个 API 端点。我想测试重定向,但是我以以下错误结束:

cy.visit() failed trying to load:

http://getmantaportal.test/authorize/

The response we received from your web server was:

  > 403: Forbidden

This was considered a failure because the status code was not 2xx.

This http request was redirected '1' time to:

  - 302: https://example.com/api/?token=eyJ0eXAiOiJKV1QiL

这是我的代码:

 it('Redirect to Manta Edge if already logged in.', function () {

    // Login first.
    // [...]

    // Go to the endpoint and get redirected immediately.
    cy.visit('/authorize/')
    cy.url().should('contain', '/api/?token=')

  })

非常感谢。

【问题讨论】:

    标签: cypress


    【解决方案1】:

    使用嵌套的cy.request 调用,您需要知道所有要通过身份验证的重定向。 请参阅下面的假设示例。

    cy.request({
        method: "POST",
        url: `YOUR FIRST URL`,
        headers: {},
        body: {
          username: YOUR_USERNAME,
          password: YOUR_PASSWORD
        },
      }).then(function (response) {
        var body = response.body;
        cy.request({
          method: "GET",
          url: `YOUR SECOND URL`,
          auth: {
            bearer: body.access_token,
          },
        }).then(function (response) {
    
          cy.request({
            method: "POST",
            url: `YOUR THIRD URL IF EXISTS`,
            auth: {
              bearer: body.access_token,
            },
            
          }).then(function (response) {
            response.headers
            // If necessary, get the cookies to finish the authentication
        });
      });
    

    【讨论】:

      猜你喜欢
      • 2023-02-14
      • 2021-10-20
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 2021-11-06
      相关资源
      最近更新 更多