Test Overview
This test logs in using a token-based authentication and verifies that the user dashboard loads successfully.
This test logs in using a token-based authentication and verifies that the user dashboard loads successfully.
describe('Token-based authentication test', () => { it('logs in with token and verifies dashboard', () => { // Set token in local storage cy.visit('/'); cy.window().then(win => { win.localStorage.setItem('authToken', 'fake-jwt-token-123'); }); // Reload to apply token cy.reload(); // Navigate to dashboard cy.visit('/dashboard'); // Check if dashboard heading is visible cy.get('h1').should('contain.text', 'User Dashboard'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Visit the home page | Browser opens at '/' with empty local storage | — | PASS |
| 2 | Set 'authToken' in local storage with a fake JWT token | Local storage now contains 'authToken' with value 'fake-jwt-token-123' | — | PASS |
| 3 | Reload the page to apply the token | Page reloads, token is present in local storage | — | PASS |
| 4 | Navigate to '/dashboard' page | Browser navigates to '/dashboard' page | — | PASS |
| 5 | Find the heading element and check its text contains 'User Dashboard' | Dashboard page is loaded with heading visible | Verify heading text contains 'User Dashboard' | PASS |
token after the test runs?
let token = '';
cy.request('POST', '/api/login', { username: 'user', password: 'pass' })
.then((response) => {
token = response.body.token;
});cy.request('POST', '/api/login', { username: 'user', password: 'pass' })
.then((response) => {
cy.setCookie('authToken', response.token);
});A) cy.request('POST', '/api/login', {username: 'user', password: 'pass'}).then(res => {
cy.setCookie('token', res.token);
});
cy.visit('/protected');
B) cy.visit('/protected');
cy.request('POST', '/api/login', {username: 'user', password: 'pass'}).then(res => {
cy.setCookie('token', res.body.token);
});
C) cy.request('POST', '/api/login', {username: 'user', password: 'pass'}).then(res => {
cy.setCookie('token', res.body.token);
cy.visit('/protected');
});
D) cy.setCookie('token', 'dummy');
cy.visit('/protected');Pass the test and this pattern moves unknown