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'
}
})
})
});
添加回答
舉報