Complete the code to send a POST request for login using cy.request.
cy.request({ method: '[1]', url: '/login', body: { username: 'user', password: 'pass' } })The login request must use the POST method to send credentials securely.
Complete the code to check that the login response status is 200.
cy.request('POST', '/login', { username: 'user', password: 'pass' }).then((response) => { expect(response.status).to.equal([1]) })
A successful login response usually returns status code 200.
Complete the code to correctly store the accessToken as a cookie after login.
cy.request('POST', '/login', { username: 'user', password: 'pass' }).then((response) => { cy.setCookie('token', response.body.[1]) })
The token is usually returned as accessToken in the response body for authorization.
Fill both blanks to create a custom command for programmatic login.
Cypress.Commands.add('login', () => { cy.request({ method: '[1]', url: '/login', body: { username: 'user', password: 'pass' } }).then((response) => { cy.setCookie('[2]', response.body.accessToken) }) })
The login request uses POST, and the cookie name for the token is usually 'token'.
Fill all three blanks to assert the user is logged in by checking the cookie and visiting the dashboard.
cy.login().then(() => { cy.getCookie('[1]').should('exist'); cy.visit('[2]'); cy.contains('[3]').should('be.visible'); })Check the 'token' cookie exists, visit the dashboard page, and verify the welcome message is visible.