Javascript Cypress发送多个请求,并且不';不允许登录
我使用的是Javascript Cypress发送多个请求,并且不';不允许登录,javascript,cypress,Javascript,Cypress,我使用的是Cypress 4.3.0version,baseUrl=“”已在Cypress.json文件中设置。当我发送cy.request()命令时,它正在发送多个请求(请参见图1)。此外,当我观察到visit命令时,我可以看到以下原始Url、解析的Url和重定向。在这种情况下,如何使用cy.request()命令登录站点 before(()=>{ cy.visit('/').then(()=>{ cy.get('input[type="hidden"]')
Cypress 4.3.0
version,baseUrl=“”已在Cypress.json文件中设置。当我发送cy.request()
命令时,它正在发送多个请求(请参见图1)。此外,当我观察到visit命令时,我可以看到以下原始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
不知怎的,我设法找到了解决问题的办法。由于
baseUrl
具有一些路径扩展/auth/login
,因此每当我触发cy.request()时,它总是重定向回登录页面,即使凭据是正确的。控制台中还有两个请求
因此,我的方法是在第一次使用qs
参数发布cy.request()之后立即发送另一个带有GET方法的cy.request()
。从请求头中,我发现每次用户登录时都会提交一个“令牌”。
如果有其他简单的方法,请告诉我
Cypress版本:4.4.0
在beforeach()中的beforeach()
,获取“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'
}
})
})
});
不知怎的,我设法找到了解决问题的办法。由于
baseUrl
具有一些路径扩展/auth/login
,因此每当我触发cy.request()时,它总是重定向回登录页面,即使凭据是正确的。控制台中还有两个请求
因此,我的方法是在第一次使用qs
参数发布cy.request()之后立即发送另一个带有GET方法的cy.request()
。从请求头中,我发现每次用户登录时都会提交一个“令牌”。
如果有其他简单的方法,请告诉我
Cypress版本:4.4.0
在beforeach()中的beforeach()
,获取“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'
}
})
})
});
有人能就如何解决上述问题提出一些建议吗?如果有人需要查看或体验相同的问题,我已经在下面发布了我的ans。有人能就如何解决上述问题提出一些建议吗?如果有人需要查看或体验相同的问题,我已经在下面发布了我的ans。