From 1da6bc8a7abb513f015f9719a327478dc7a9dc3e Mon Sep 17 00:00:00 2001 From: epszaw Date: Fri, 24 Jan 2025 14:17:48 +0100 Subject: [PATCH 1/6] fix test start data util --- packages/allure-cypress/src/browser/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/allure-cypress/src/browser/utils.ts b/packages/allure-cypress/src/browser/utils.ts index 573a11e30..02368e4b6 100644 --- a/packages/allure-cypress/src/browser/utils.ts +++ b/packages/allure-cypress/src/browser/utils.ts @@ -30,7 +30,7 @@ export const uint8ArrayToBase64 = (data: unknown) => { export const getTestStartData = (test: CypressTest) => ({ ...getNamesAndLabels(Cypress.spec, test), - start: test.wallClockStartedAt?.getTime() || Date.now(), + start: test.wallClockStartedAt?.getTime?.() || Date.now(), }); export const getTestStopData = (test: CypressTest) => ({ From 092139a9d3cbe1bcb00647f7f3d8991fc60a0a8d Mon Sep 17 00:00:00 2001 From: epszaw Date: Mon, 27 Jan 2025 18:20:22 +0100 Subject: [PATCH 2/6] fix problem when the process stiks after local storage clean-up --- .../allure-cypress/src/browser/commandLog.ts | 22 ++++++++++++++++--- .../src/browser/events/cypress.ts | 1 + packages/allure-cypress/src/browser/state.ts | 3 +++ .../allure-cypress/test/spec/security.test.ts | 21 ++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 packages/allure-cypress/test/spec/security.test.ts diff --git a/packages/allure-cypress/src/browser/commandLog.ts b/packages/allure-cypress/src/browser/commandLog.ts index e27d5fa30..82092061c 100644 --- a/packages/allure-cypress/src/browser/commandLog.ts +++ b/packages/allure-cypress/src/browser/commandLog.ts @@ -51,6 +51,7 @@ export const setupScreenshotAttachmentStep = (originalName: string | undefined, export const startCommandLogStep = (entry: CypressLogEntry) => { const currentLogEntry = getCurrentLogEntry(); + if (typeof currentLogEntry !== "undefined" && shouldStopCurrentLogStep(currentLogEntry.log, entry)) { stopCommandLogStep(currentLogEntry.log.attributes.id); } @@ -65,12 +66,14 @@ export const stopCommandLogStep = (entryId: string) => findAndStopStepWithSubste const pushLogEntry = (entry: CypressLogEntry) => { const id = entry.attributes.id; const stepDescriptor: LogStepDescriptor = { id, type: "log", log: entry }; + pushStep(stepDescriptor); // Some properties of some Command Log entries are undefined at the time the entry is stopped. An example is the // Yielded property of some queries. We defer converting them to Allure step parameters until the test/hook ends. setupStepFinalization(stepDescriptor, (data) => { data.parameters = getCommandLogStepParameters(entry); + if (stepDescriptor.attachmentName) { // Rename the step to match the attachment name. Once the names are the same, Allure will render the // attachment in the place of the step. @@ -146,18 +149,31 @@ const getLogProps = (entry: CypressLogEntry) => { attributes: { consoleProps }, } = entry; const isAssertionWithMessage = !!maybeGetAssertionLogMessage(entry); + const props = consoleProps(); + + // accessing LocalStorage after the page reload can stick the test runner + // to avoid the issue, we just need to log the command manually + // the problem potentially can happen with other storage related commands, like `clearAllLocalStorage`, `clearAllSessionStorage`, `getAllLocalStorage`, `getAllSessionStorage`, `setLocalStorage`, `setSessionStorage` + // but probably, we don't need to silent them all at this moment + // the context: https://github.com/allure-framework/allure-js/issues/1222 + if (["clearLocalStorage"].includes(props.name)) { + return [ + ["name", "clearLocalStorage"], + ["type", "command"], + ["props", {}], + ] as [string, unknown][]; + } // For assertion logs, we interpolate the 'Message' property, which contains unformatted assertion description, // directly into the step's name. // No need to keep the exact same information in the step's parameters. - return Object.entries(consoleProps().props).filter( - ([k, v]) => isDefined(v) && !(isAssertionWithMessage && k === "Message"), - ); + return Object.entries(props).filter(([k, v]) => isDefined(v) && !(isAssertionWithMessage && k === "Message")); }; const maybeGetAssertionLogMessage = (entry: CypressLogEntry) => { if (isAssertLog(entry)) { const message = entry.attributes.consoleProps().props.Message; + if (message && typeof message === "string") { return message; } diff --git a/packages/allure-cypress/src/browser/events/cypress.ts b/packages/allure-cypress/src/browser/events/cypress.ts index fd0514d00..969330e83 100644 --- a/packages/allure-cypress/src/browser/events/cypress.ts +++ b/packages/allure-cypress/src/browser/events/cypress.ts @@ -16,6 +16,7 @@ const onAfterScreenshot = ( ...[, { name: originalName, path }]: Parameters ) => { const name = originalName ?? getFileNameFromPath(path); + reportScreenshot(path, name); setupScreenshotAttachmentStep(originalName, name); }; diff --git a/packages/allure-cypress/src/browser/state.ts b/packages/allure-cypress/src/browser/state.ts index fb6a2154d..aa99d987e 100644 --- a/packages/allure-cypress/src/browser/state.ts +++ b/packages/allure-cypress/src/browser/state.ts @@ -3,6 +3,7 @@ import { DEFAULT_RUNTIME_CONFIG, last, toReversed } from "../utils.js"; export const getAllureState = () => { let state = Cypress.env("allure") as AllureSpecState; + if (!state) { state = { config: DEFAULT_RUNTIME_CONFIG, @@ -15,8 +16,10 @@ export const getAllureState = () => { stepsToFinalize: [], nextApiStepId: 0, }; + Cypress.env("allure", state); } + return state; }; diff --git a/packages/allure-cypress/test/spec/security.test.ts b/packages/allure-cypress/test/spec/security.test.ts new file mode 100644 index 000000000..608343d2e --- /dev/null +++ b/packages/allure-cypress/test/spec/security.test.ts @@ -0,0 +1,21 @@ +import { expect, it } from "vitest"; +import { Stage, Status } from "allure-js-commons"; +import { runCypressInlineTest } from "../utils.js"; + +it("shouldn't break the flow when access storage after the page reload", async () => { + const { tests } = await runCypressInlineTest({ + "cypress/e2e/sample.cy.js": () => ` + it("passed", () => { + cy.visit("https://allurereport.org"); + cy.clearLocalStorage(); + cy.wait(200); + cy.reload(); + cy.wait(200); + }); + `, + }); + + expect(tests).toHaveLength(1); + expect(tests[0].status).toBe(Status.PASSED); + expect(tests[0].stage).toBe(Stage.FINISHED); +}); From d75284bf81e22fdd8e73127e6f6d993c0ea96c22 Mon Sep 17 00:00:00 2001 From: epszaw Date: Tue, 28 Jan 2025 11:06:07 +0100 Subject: [PATCH 3/6] fix broken tests --- packages/allure-cypress/src/browser/commandLog.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/allure-cypress/src/browser/commandLog.ts b/packages/allure-cypress/src/browser/commandLog.ts index 82092061c..c996bfe25 100644 --- a/packages/allure-cypress/src/browser/commandLog.ts +++ b/packages/allure-cypress/src/browser/commandLog.ts @@ -149,14 +149,14 @@ const getLogProps = (entry: CypressLogEntry) => { attributes: { consoleProps }, } = entry; const isAssertionWithMessage = !!maybeGetAssertionLogMessage(entry); - const props = consoleProps(); + const { props, name } = consoleProps(); // accessing LocalStorage after the page reload can stick the test runner // to avoid the issue, we just need to log the command manually // the problem potentially can happen with other storage related commands, like `clearAllLocalStorage`, `clearAllSessionStorage`, `getAllLocalStorage`, `getAllSessionStorage`, `setLocalStorage`, `setSessionStorage` // but probably, we don't need to silent them all at this moment // the context: https://github.com/allure-framework/allure-js/issues/1222 - if (["clearLocalStorage"].includes(props.name)) { + if (["clearLocalStorage"].includes(name)) { return [ ["name", "clearLocalStorage"], ["type", "command"], From 1a40443f1164a8c250825bfbc354bb7c30c3b8b7 Mon Sep 17 00:00:00 2001 From: epszaw Date: Tue, 28 Jan 2025 11:20:43 +0100 Subject: [PATCH 4/6] review fixes --- packages/allure-cypress/src/browser/commandLog.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/allure-cypress/src/browser/commandLog.ts b/packages/allure-cypress/src/browser/commandLog.ts index c996bfe25..fb587e74f 100644 --- a/packages/allure-cypress/src/browser/commandLog.ts +++ b/packages/allure-cypress/src/browser/commandLog.ts @@ -157,11 +157,7 @@ const getLogProps = (entry: CypressLogEntry) => { // but probably, we don't need to silent them all at this moment // the context: https://github.com/allure-framework/allure-js/issues/1222 if (["clearLocalStorage"].includes(name)) { - return [ - ["name", "clearLocalStorage"], - ["type", "command"], - ["props", {}], - ] as [string, unknown][]; + return [] as [string, unknown][]; } // For assertion logs, we interpolate the 'Message' property, which contains unformatted assertion description, From 6dcc2e32863c225de360bec85c4a2d2ab93c5548 Mon Sep 17 00:00:00 2001 From: epszaw Date: Tue, 28 Jan 2025 12:23:40 +0100 Subject: [PATCH 5/6] better start time fix Co-authored-by: Maksim Stepanov <17935127+delatrie@users.noreply.github.com> --- packages/allure-cypress/src/browser/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/allure-cypress/src/browser/utils.ts b/packages/allure-cypress/src/browser/utils.ts index 02368e4b6..8aec4bd4c 100644 --- a/packages/allure-cypress/src/browser/utils.ts +++ b/packages/allure-cypress/src/browser/utils.ts @@ -30,7 +30,7 @@ export const uint8ArrayToBase64 = (data: unknown) => { export const getTestStartData = (test: CypressTest) => ({ ...getNamesAndLabels(Cypress.spec, test), - start: test.wallClockStartedAt?.getTime?.() || Date.now(), + start: typeof test.wallClockStartedAt === "string" ? Date.parse(test.wallClockStartedAt) : test.wallClockStartedAt?.getTime?.() || Date.now(), }); export const getTestStopData = (test: CypressTest) => ({ From 6e795bf00ccbae011f4606c7bf2b5fd0c2ad8e7d Mon Sep 17 00:00:00 2001 From: epszaw Date: Tue, 28 Jan 2025 12:53:25 +0100 Subject: [PATCH 6/6] fix linters issues --- packages/allure-cypress/src/browser/utils.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/allure-cypress/src/browser/utils.ts b/packages/allure-cypress/src/browser/utils.ts index 8aec4bd4c..2f9615589 100644 --- a/packages/allure-cypress/src/browser/utils.ts +++ b/packages/allure-cypress/src/browser/utils.ts @@ -30,7 +30,10 @@ export const uint8ArrayToBase64 = (data: unknown) => { export const getTestStartData = (test: CypressTest) => ({ ...getNamesAndLabels(Cypress.spec, test), - start: typeof test.wallClockStartedAt === "string" ? Date.parse(test.wallClockStartedAt) : test.wallClockStartedAt?.getTime?.() || Date.now(), + start: + typeof test.wallClockStartedAt === "string" + ? Date.parse(test.wallClockStartedAt) + : test.wallClockStartedAt?.getTime?.() || Date.now(), }); export const getTestStopData = (test: CypressTest) => ({