0
0
Cypresstesting~10 mins

Programmatic login (cy.request) in Cypress - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to send a POST request for login using cy.request.

Cypress
cy.request({ method: '[1]', url: '/login', body: { username: 'user', password: 'pass' } })
Drag options to blanks, or click blank then click option'
APOST
BPUT
CGET
DDELETE
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for sending login data.
2fill in blank
medium

Complete the code to check that the login response status is 200.

Cypress
cy.request('POST', '/login', { username: 'user', password: 'pass' }).then((response) => { expect(response.status).to.equal([1]) })
Drag options to blanks, or click blank then click option'
A500
B201
C404
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Expecting 201 or other status codes instead of 200.
3fill in blank
hard

Complete the code to correctly store the accessToken as a cookie after login.

Cypress
cy.request('POST', '/login', { username: 'user', password: 'pass' }).then((response) => { cy.setCookie('token', response.body.[1]) })
Drag options to blanks, or click blank then click option'
Atoken
BaccessToken
CauthToken
Dpassword
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property name like 'password' or 'authToken'.
4fill in blank
hard

Fill both blanks to create a custom command for programmatic login.

Cypress
Cypress.Commands.add('login', () => { cy.request({ method: '[1]', url: '/login', body: { username: 'user', password: 'pass' } }).then((response) => { cy.setCookie('[2]', response.body.accessToken) }) })
Drag options to blanks, or click blank then click option'
APOST
BGET
Ctoken
Dauth
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET method or wrong cookie name like 'auth'.
5fill in blank
hard

Fill all three blanks to assert the user is logged in by checking the cookie and visiting the dashboard.

Cypress
cy.login().then(() => { cy.getCookie('[1]').should('exist'); cy.visit('[2]'); cy.contains('[3]').should('be.visible'); })
Drag options to blanks, or click blank then click option'
Atoken
B/dashboard
CWelcome
D/login
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong cookie, visiting login page instead of dashboard, or wrong text.