question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add failed screenshot to test information for custom reporter

See original GitHub issue

Current behavior:

When writing a custom reporter you have access to some information from the test, but no indication of the screenshot taken automatically on failure.

runner.on('test end', function(test) {
  console.log(test);
});

Desired behavior:

the test object to have the screenshot path

runner.on('test end', function(test) {
  console.log(test.screenshot);
  // or
  console.log(test.failedScreenshot);
});

Steps to reproduce: (app code and test code)

simple runner reporters/test.js

module.exports = function(runner, config) {
  runner.on('test end', function(test) {
    console.log('test done');
  });
}

use in cypress.json

{
  "reporter": "reporters/test.js"
}

Versions

Cypress 3.3.1
OS: Linux (Ubuntu) Browser: Any

Workaround

You can access the files with fs and path no problem in the end hook. This is less than ideal as you have to try and map file names to your failures

runner.on('end', function() {
  //getFilePaths is a helper to list all files in a directory
  console.log('end:', getFilePaths('cypress/screenshots'));
});

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
Shelexcommented, Jun 28, 2019

@jennifer-shehane Yeah, have added screenshots to mochawesome using mapping filename approach and it was a long road of defining how screenshot names behave with special symbols (some are supported, other deleted) and after switch from 3.2 to 3.3 it has changed >< So it would be great to have it accessible inside test object.

3reactions
hemchecommented, Jul 14, 2020

@irfancharania Here is the one that works even if there are failures in any of the hooks as well.

Cypress.on('test:after:run', (test, runnable ) => {
if (test.state === 'failed') {
  let item = runnable
  const nameParts = [runnable.title]
  while (item.parent) {
    nameParts.unshift(item.parent.title)
    item = item.parent
  }
  if(runnable.hookName) {
    nameParts.push(`${runnable.hookName} hook`)
    }
  const fullTestName = nameParts.filter(Boolean).join(' -- ')
  const screenshotPath = `cypress/screenshots/${Cypress.spec.name}/${fullTestName} (failed).png`.replace("  "," ")

  addContext({ test }, screenshotPath)
}
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add Screenshot in Extent Report for Failed Test Cases ...
How to add Screenshot in Extent Report for Failed Test Cases in Selenium:Learn:+ How to generate Extent Report +How to add screenshot for ......
Read more >
Extent Reports - Screenshots of Failed Test Cases in Extent ...
Insert Screenshots of a Failed Test Cases in Extent Reports: In this post, we see how to add Screenshots in Extent Reports –...
Read more >
How can I include a failure screenshot to the testNG report
Yes, you can include the link to your screenshot in testng report. ... Then you can find this link under the failed testcases...
Read more >
How to add Screenshots to Jest report. | by Harry Hou - Medium
You know we cannot add any custom data to the test results. The only way is that we save all Info of the...
Read more >
How to Attach Cypress Screenshots to the Mochawesome ...
In this article, we will look at how to add the failed test screenshot to the Mochawesome report and set a custom screen...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found