-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cypress evidence improvements (#1065)
- Loading branch information
1 parent
a2ab9a7
commit 31f4453
Showing
14 changed files
with
205 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { createHash } from 'crypto'; | ||
import { writeFile } from 'fs/promises'; | ||
import * as sharp from 'sharp'; | ||
import { ScreenshotEvidenceData, ScreenshotEvidenceResult } from './screenshot.types'; | ||
|
||
const SCREENSHOT_METADATA = { | ||
height: 50, | ||
margin: 10, | ||
textAlign: 'left', | ||
textColor: '#000', | ||
textSize: 'large', | ||
backgroundColor: '#fff', | ||
maxNameLength: 100, | ||
} as const; | ||
const EVIDENCE_HASH_ALGORITHM = 'sha256' as const; | ||
|
||
export const addEvidenceMetaToScreenshot = async (data: ScreenshotEvidenceData): Promise<ScreenshotEvidenceResult> => { | ||
const metadata: [string, string][] = [ | ||
['Timestamp', data.takenAt], | ||
['Testname', data.name.substring(0, SCREENSHOT_METADATA.maxNameLength)], | ||
['Step', data.step.toString()], | ||
['Screenshot', data.subStep.toString()], | ||
['Build number', process.env.BUILD_NUMBER ?? '-'], | ||
['Git commit', process.env.COMMIT_INFO_SHA ?? '-'], | ||
]; | ||
|
||
const image = sharp(data.path); | ||
const imageMetadata = await image.metadata(); | ||
const imageBuffer = await image | ||
.resize({ | ||
background: SCREENSHOT_METADATA.backgroundColor, | ||
fit: 'contain', | ||
height: (imageMetadata.height ?? 0) + SCREENSHOT_METADATA.height, | ||
position: 'bottom', | ||
width: imageMetadata.width ?? 0, | ||
}) | ||
.composite([ | ||
{ | ||
input: { | ||
text: { | ||
align: SCREENSHOT_METADATA.textAlign, | ||
rgba: true, | ||
text: metadata | ||
.map(([key, value]) => `<span foreground="${SCREENSHOT_METADATA.textColor}" size="${SCREENSHOT_METADATA.textSize}"><b>${key}</b>: ${value}</span>`) | ||
.join('\t'), | ||
width: (imageMetadata.width ?? 0) - SCREENSHOT_METADATA.margin * 2, | ||
}, | ||
}, | ||
left: SCREENSHOT_METADATA.margin, | ||
top: SCREENSHOT_METADATA.margin, | ||
}, | ||
]) | ||
.toBuffer(); | ||
await writeFile(data.path, imageBuffer); | ||
|
||
const hash = createHash(EVIDENCE_HASH_ALGORITHM).update(imageBuffer).digest('hex'); | ||
return { | ||
hash, | ||
path: data.path, | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export type ScreenshotEvidenceData = { | ||
name: string; | ||
path: string; | ||
step: number; | ||
subStep: number; | ||
takenAt: string; | ||
}; | ||
|
||
export type ScreenshotEvidenceResult = { | ||
hash: string; | ||
path: string; | ||
}; | ||
export const isScreenshotEvidenceResult = (candidate: unknown): candidate is ScreenshotEvidenceResult => | ||
Boolean( | ||
typeof candidate === 'object' && | ||
candidate && | ||
'hash' in candidate && | ||
'path' in candidate && | ||
typeof candidate.hash === 'string' && | ||
typeof candidate.path === 'string' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,70 @@ | ||
export const printTestEvidence = (testName: string, testStep: number, selector: string, description: string) => { | ||
import * as path from 'path'; | ||
import { isScreenshotEvidenceResult, ScreenshotEvidenceData } from "../plugins/screenshot.types"; | ||
import { consoleLogs } from "./e2e"; | ||
|
||
const logEvidence = (name: string, step: number, description: string, evidenceLogs: string[]) => { | ||
cy.url().then(url => { | ||
const logs: string[] = []; | ||
logs.push('====================================='); | ||
logs.push(`Testname: ${name} // step: ${step}`); | ||
logs.push(`URL: ${url}`); | ||
logs.push(`Description: ${description}`); | ||
logs.push('----- Test Evidence starts here ----'); | ||
logs.push(...evidenceLogs); | ||
logs.push('----- Test Evidence ends here ----'); | ||
consoleLogs.push(...logs); | ||
cy.task('log', logs.join('\n')); | ||
}); | ||
} | ||
|
||
export const printTestDOMEvidence = (testName: string, testStep: number, selector: string, description: string) => { | ||
if (!selector) { | ||
throw new Error('selector must not NOT be undefined'); | ||
} | ||
cy.task('log', '====================================='); | ||
cy.task('log', 'Testname: ' + testName + ' // step: ' + testStep); | ||
cy.url().then(urlString => { | ||
cy.task('log', 'URL: ' + urlString); | ||
}); | ||
cy.task('log', 'Description: ' + description); | ||
cy.task('log', '----- Test Evidence starts here ----'); | ||
cy.get(selector).then($selectedElement => { | ||
cy.task('log', 'Selector: ' + selector + '\r ' + $selectedElement.get(0).outerHTML); | ||
logEvidence(testName, testStep, description, [`Selector: ${selector}\n ${$selectedElement.get(0).outerHTML}`]); | ||
}); | ||
}; | ||
|
||
export const printTestPlainEvidence = (testName: string, testStep: number, expectedValue: string, actualValue: string, description: string) => { | ||
if (!expectedValue || !actualValue) { | ||
throw new Error('expectedValue and actualValue must not NOT be undefined'); | ||
} | ||
logEvidence(testName, testStep, description, [ | ||
`Expected Result:\n ${String(expectedValue)}`, | ||
`Actual Result:\n ${String(actualValue)}` | ||
]); | ||
}; | ||
|
||
export const takeScreenshotEvidence = (testName: string, testStep: number, testSubStep: number = 1, description: string, skipMeta = false) => { | ||
cy.wrap(null).then(() => { | ||
const data: Omit<ScreenshotEvidenceData, 'path' | 'takenAt'> & | ||
Partial<Pick<ScreenshotEvidenceData, 'path' | 'takenAt'>> = { | ||
name: testName, | ||
step: testStep, | ||
subStep: testSubStep, | ||
}; | ||
|
||
cy.screenshot(`testevidence_${testName}_${testStep}_${testSubStep}`, { | ||
onAfterScreenshot(_, { path, takenAt }) { | ||
data.path = path; | ||
data.takenAt = new Date(takenAt).toISOString(); | ||
}, | ||
}) | ||
.then<unknown>(() => { | ||
if (!data.path || !data.takenAt || skipMeta) { | ||
return null; | ||
} | ||
cy.task('takeScreenshotEvidence', data); | ||
}) | ||
.then((result) => { | ||
if (!isScreenshotEvidenceResult(result)) { | ||
return null; | ||
} | ||
|
||
logEvidence(testName, testStep, description, [ | ||
`Stored screenshot "${path.basename(result.path)}" with hash (sha256) ${result.hash} taken at ${String(data.takenAt)} as evidence.` | ||
]); | ||
}); | ||
}); | ||
cy.task('log', '----- Test Evidence ends here ----'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters