亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

賽普拉斯發送多個請求并且不允許登錄

賽普拉斯發送多個請求并且不允許登錄

catspeake 2022-06-16 10:28:36
我正在使用Cypress 4.3.0版本,baseUrl = " https://tenant-demo.somesitedev.net " 已在 cypress.json 文件中設置。當我發送cy.request()命令時,它正在發送多個請求(請參見圖:1)。此外,當我觀察訪問命令時,我可以看到原始 URL、已解析 URL 和重定向。在這種情況下,我如何使用cy.request()命令登錄到站點。before(()=>{    cy.visit('/').then(()=>{        cy.get('input[type="hidden"]').invoke('val').then((val)=>{                const token = val;                cy.login(token);        })      })    }) Cypress.Commands.add('login', (token) => {    const username= 'test1.user';    const password= 'somepassword';    const accessToken = localStorage.getItem('tokens');    const cookieValue = document.cookie.split(';');    const configCat = localStorage.getItem('ConfigCat_');  cy.request({      method: 'GET',      url: '/dashboard',      failOnStatusCode: false,      form: true,      body:{        _token: token,        username,        password      },      headers: {        'accept': 'text/html',        'content-type': 'application/x-www-form-urlencoded',        'authorization': `bearer ${accessToken}`,        'ConfigCat_': `${configCat}`,        'cookie': `${cookieValue}`       }     }).then((res)=>{      visitDashboard();     })  })  const visitDashboard = () => {    cy.visit('dashboard')  }圖。1圖:2
查看完整描述

1 回答

?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

不知何故,我設法找到了解決問題的方法。由于baseUrl有一些路徑擴展名/auth/login,每當我觸發 cy.request() 時,即使憑據正確,它總是會重定向回登錄頁面。控制臺中還有兩個請求。


所以我所做的方法是在第一個帶有參數的 POST cy.request() 之后立即發送另一個cy.request()帶有參數的 GET 方法。從請求標頭中,我發現每次用戶登錄時都會提交一個“令牌”。如果有另一種簡單的方法讓我知道。bodyqs


賽普拉斯版本:4.4.0


在里面beforeEach(),獲取 'token' 值;


 beforeEach(() => {

    cy.visit('/');

    cy.loadTokens();

    cy.get('input[name="_token"]').invoke('val').then((val)=>{

        const tokenValue = val;

        cy.loginRequest(tokenValue);

      })

    })

以下是commands.js文件:


Cypress.Commands.add('loginRequest', function (tokenValue) {

     return cy.request({

      method: 'POST',

      url: Cypress.config('baseUrl'),

      followRedirect: true,

      headers: {

        'content-type': 'text/html; charset=UTF-8'

      },

      qs:{

        _token: tokenValue,

        username: 'your_username',

        password:'your_password'

      }

    }).then(()=>{

      return cy.request({

        method: 'GET',

        url: 'https://tenant-demo.somesitedev.net/dashboard',

        followRedirect: false,

        headers: {

          'content-type': 'text/html; charset=UTF-8'

        },

        body:{

          _token: tokenValue,

          username: 'your_username',

          password:'your_password'

        }

      })

    })

  });


查看完整回答
反對 回復 2022-06-16
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號