对于我的应用程序,我尝试通过API而不是UI登录
我需要存储在应用程序中导航的accessToken
我当前的登录方法如下所示
Cypress.Commands.add('login', (overrides = {}) => {
Cypress.log({
name: 'loginViaAuth0',
});
const options = {
method: 'POST',
url: Cypress.env('auth_url'),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: {
username: Cypress.env('auth_username'),
password: Cypress.env('auth_password'),
}
}
cy.request(options);
});我需要将accessToken存储到资源文件中。我尝试过像这里这样的各种方法,但都没有成功
谢谢
编辑:
我试过了,但还是不走运
Cypress.Commands.add('login', (overrides = {}) => {
cy.request({
method: 'POST',
url: Cypress.env('auth_url'),
body: {
user: {
email: Cypress.env('auth_username'),
password: Cypress.env('auth_password'),
}
}
})
.its('token')
.then((token) => {
window.localStorage.setItem('accessToken', token);
});
});编辑:这对任何感兴趣的人来说最终都是有效的
Cypress.Commands.add('login', (overrides = {}) => {
cy.request({
method: 'POST',
url: Cypress.env('auth_url'), form: true,
body: { grant_type: 'client_credentials', scope: 'xero_all-apis' ,
username: Cypress.env('auth_username'),
password: Cypress.env('auth_password'), } })
.its('body')
.then(res => {
cy.setLocalStorage('accessToken',res.accessToken);
});
});发布于 2021-01-07 12:07:09
@Steven983,请尝试这个
Cypress code Cypress.Commands.add('login', () => {
cy.request({
method: 'POST',
url: Cypress.env('auth_url'), form: true,
body: { grant_type: 'client_credentials', scope: 'xero_all-apis' },
auth: {
username: Cypress.env('auth_username'),
password: Cypress.env('auth_password'), } })
.its('body')
.then(res => {
cy.log(res.body.accessToken) //To verify token is shown in console
cy.setLocalStorage('accessToken', res.body.accessToken);
}); });
https://stackoverflow.com/questions/65576848
复制相似问题