In my cypress test I am using cy.readFile()
to pick up screenshots of failing tests like so:
afterEach(() => {
const screenshotsFolder = 'test-results/screenshots'
const screenshotFileName = `${testState.feature.name} -- ${testState.currentScenario.name} (failed).png`;
cy.readFile(
`${screenshotsFolder}/${Cypress.spec.name}/${screenshotFileName}`,
"base64"
).then((imgData) => {
stepResult.attachment = {
data: imgData,
media: { type: "image/png" },
index: testState.currentStep,
testCase: testState.formatTestCase(testState.currentScenario),
};
});
});
When I run a single test, the screenshot is saved like so, & is being read successfully:
test-results/screenshots/login.feature/Login Feature -- Valid Credentials Scenario.png
However, if I run multiple tests, then the files are stored like this:
test-results/screenshots/Smoke/01-Dummy/login.feature/Login Feature -- Valid Credentials Scenario.png
test-results/screenshots/Regression/02-Regression/login.feature/Login Feature -- Invalid Credentials Scenario.png
test-results/screenshots/Performance/03-Performance/login.feature/Login Feature -- Invalid Credentials Scenario.png
Therefore, I need to update the path in my cy.readFile()
to look for the PNG files anywhere within the screenshots
folder (i.e. in sub-directories).
I tried adding **
to the filepath, but that hasn't worked:
cy.readFile(`test-results/screenshots/**/${Cypress.spec.name}/${screenshotFileName}`)
Can someone please show me how to read all PNG files within the screenshots
folder?