For my application I'm trying to login via the API and not the UI
I'm required to store the accessToken to navigate through the application
My current login method looks like this
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);
});
I need to store the accessToken to the resource file. I have tried various methods like on here but without success
Thank you
Edit:
I have tried this but still no luck
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);
});
});
Edit: this worked in the end for anybody interested
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);
});
});