Skip to content

Commit 6fed343

Browse files
committed
test(cypress): preserve live quiz state across relogin
1 parent 3453b45 commit 6fed343

3 files changed

Lines changed: 39 additions & 18 deletions

File tree

cypress/cypress/e2e/O-live-quiz-workflow.cy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ describe('Different live-quiz workflows', function () {
10651065
it('Test the live quiz functionalities on mobile devices', function () {
10661066
// login student again on mobile, test navigation and answer second question
10671067
cy.viewport('iphone-x')
1068-
cy.loginStudent()
1068+
cy.loginStudent({ preserveClientState: true })
10691069
cy.findByText(this.data.course2.quiz.displayName).should('exist').click()
10701070
cy.findByText(this.data.NR.content, { timeout: 10000 }).should('exist')
10711071
cy.findByText(messages.pwa.liveQuiz.allQuestionsAnswered).should(

cypress/cypress/support/commands.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -299,27 +299,44 @@ Cypress.Commands.add('logoutUser', () => {
299299
cy.clearCookie('next-auth.session-token')
300300
})
301301

302-
Cypress.Commands.add('loginStudent', () => {
303-
cy.loginStudentPassword({ username: Cypress.env('STUDENT_USERNAME') })
302+
interface StudentLoginOptions {
303+
preserveClientState?: boolean
304+
}
305+
306+
Cypress.Commands.add('loginStudent', (options: StudentLoginOptions = {}) => {
307+
cy.loginStudentPassword({
308+
username: Cypress.env('STUDENT_USERNAME'),
309+
preserveClientState: options.preserveClientState,
310+
})
304311
})
305312

306313
Cypress.Commands.add(
307314
'loginStudentPassword',
308-
({ username }: { username: string }) => {
315+
({
316+
username,
317+
preserveClientState = false,
318+
}: { username: string } & StudentLoginOptions) => {
309319
cy.clearAllCookies()
310320
cy.clearAllLocalStorage()
311-
cy.visit(Cypress.env('URL_STUDENT_LOGIN'), {
312-
onBeforeLoad: (win) => {
313-
win.localStorage.clear()
314-
win.sessionStorage.clear()
315-
try {
316-
win.indexedDB?.deleteDatabase('localforage')
317-
} catch {
318-
// Fall back to the regular localforage clear after load.
319-
}
320-
},
321-
})
322-
clearPersistedClientState()
321+
cy.visit(
322+
Cypress.env('URL_STUDENT_LOGIN'),
323+
preserveClientState
324+
? undefined
325+
: {
326+
onBeforeLoad: (win) => {
327+
win.localStorage.clear()
328+
win.sessionStorage.clear()
329+
try {
330+
win.indexedDB?.deleteDatabase('localforage')
331+
} catch {
332+
// Fall back to the regular localforage clear after load.
333+
}
334+
},
335+
}
336+
)
337+
if (!preserveClientState) {
338+
clearPersistedClientState()
339+
}
323340
cy.get('[data-cy="username-field"]').click().type(username)
324341
cy.get('[data-cy="password-field"]')
325342
.click()
@@ -1844,9 +1861,12 @@ declare global {
18441861
loginInstitutionalCatalyst3(): Chainable<void>
18451862
loginInstitutionalCatalyst4(): Chainable<void>
18461863
logoutUser(): Chainable<void>
1847-
loginStudent(): Chainable<void>
1864+
loginStudent(options?: StudentLoginOptions): Chainable<void>
18481865
loginAssessmentStudent(): Chainable<void>
1849-
loginStudentPassword({ username }: { username: string }): Chainable<void>
1866+
loginStudentPassword({
1867+
username,
1868+
preserveClientState,
1869+
}: { username: string } & StudentLoginOptions): Chainable<void>
18501870
createAnswerCollection({
18511871
name,
18521872
description,

project/CODEBASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ This is a **living document**: when you discover such a pattern during a task, a
4949

5050
- **Test environment caveats**: `pnpm run test:run` triggers Cypress which needs a running DB + seeded data. `pnpm --filter @klicker-uzh/graphql test` needs `HATCHET_CLIENT_TOKEN`. For verifying non-DB changes locally, target specific packages (e.g., `pnpm --filter @klicker-uzh/grading test`, `pnpm --filter @klicker-uzh/util test`).
5151
- **Cypress CI signal timing**: `cypress: default-group (merge)` can report an increasing failed-test count while `cypress-run-cloud` is still in progress; wait for `cypress-run-cloud` completion before expecting downloadable GitHub job logs. (`.github/workflows/cypress-testing.yml`)
52+
- **Cypress student login state**: `cy.loginStudent()` and `cy.loginStudentPassword()` clear localforage by default for test isolation. Live quiz continuation tests that intentionally rely on stored unanswered-question state should pass `{ preserveClientState: true }`. (`cypress/cypress/support/commands.ts`, `cypress/cypress/e2e/O-live-quiz-workflow.cy.ts`)
5253

5354
## Process & tooling
5455

0 commit comments

Comments
 (0)