Skip to content

Instantly share code, notes, and snippets.

@przemuh
Created July 16, 2020 07:10
Show Gist options
  • Save przemuh/08e29454ec276019009648552433b4eb to your computer and use it in GitHub Desktop.
Save przemuh/08e29454ec276019009648552433b4eb to your computer and use it in GitHub Desktop.
import addContext from "mochawesome/addContext";
Cypress.Screenshot.defaults({
// Disable default screenshot on fail because we want to change the filename
screenshotOnRunFailure: false,
});
function generateScreenshotName(test) {
const MAX_SPEC_NAME_LENGTH = 220;
return [
test.fullTitle().slice(0, MAX_SPEC_NAME_LENGTH),
" (failed) ",
test.hookName,
]
.filter(Boolean)
.join("")
.trim();
}
afterEach(function() {
if (this.currentTest.state === "failed") {
cy.screenshot(generateScreenshotName(this.currentTest), {
capture: "runner",
});
}
});
Cypress.on("test:after:run", (test, runnable) => {
addContext({ test }, `${generateScreenshotName(runnable)}.png`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment