I want to login and set a localStorage
token on the client (specifically jwt
)
How can I accomplish this using cy.request
, as suggested in the Cypress Documentation?
Here's an example of adding a command cy.login()
that you can use in any Cypress test, or put in a beforeEach
hook.
Cypress.Commands.add('login', () => { cy.request({ method: 'POST', url: 'http://localhost:3000/api/users/login', body: { user: { email: '[email protected]', password: 'jakejake', } } }) .then((resp) => { window.localStorage.setItem('jwt', resp.body.user.token) }) })
Then in your test:
beforeEach(() => { cy.login() })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With