CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

Is there a way to add screenshot to JUnit report as attachment?

Open iljao opened this issue 7 years ago • 4 comments

Hi

I'd like to generate a JUnit report for my tests, and include a screenshot, which is automatically saved by codeceptjs on fail. Searching didn't give any results. But maybe there is a simple way to achieve this though?

This is a part of my config describing reporter options:

  helpers: {
    Puppeteer: {
      uniqueScreenshotNames: true,
      [...]
    }
  },
  mocha: {
    reporterOptions: {
      codeceptjs-cli-reporter: {
        stdout: "-",
        options: {
          debug: true
        }
      },
      mocha-junit-reporter: {
        stdout: "./output/console.log",
        options: {
          mochaFile: "./output/result.xml"
        }
      }
    }
  },

here's how I run tests: codeceptjs run --reporter=mocha-multi

iljao avatar Feb 14 '19 16:02 iljao

@iljao well, I took some times to look at this, there are several things to make this works:

  1. The current npm of this repohttps://github.com/michaelleeallen/mocha-junit-reporter is not updated, which means attachments will not work -> https://github.com/michaelleeallen/mocha-junit-reporter/commit/36ea8b8ee95f8a3c3950d2dd7a19f2ea58f453c1#diff-168726dbe96b3ce427e7fedce31bb0bcR23 -> to solve this, you need either wait for the author to publish the latest version or clone it ;)

  2. We need also to turn it on from codeceptjs side, the config should be like this

mocha: {
        reporterOptions: {
          'codeceptjs-cli-reporter': {
            stdout: "-",
            options: {
              debug: true
            }
          },
          'mocha-junit-reporter': {
            stdout: "./test/codeceptjs/output/console.log",
            options: {
              mochaFile: "./test/codeceptjs/output/result.xml"
            },
            attachments: true // <<< this line
          }
        }
      },

and also there is a change in the core of the this file screenshotOnFail.js

if (Container.mocha().options.reporterOptions['mocha-junit-reporter'] && Container.mocha().options.reporterOptions['mocha-junit-reporter'].attachments) {
          test.attachments = [path.join(global.output_dir, fileName)];
        };

kobenguyent avatar Mar 26 '19 11:03 kobenguyent

in the end, it will work when everything is changed.

<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Mocha Tests" time="0.989" tests="1" failures="1">
  <testsuite name="Root Suite" timestamp="2019-03-26T11:05:48" tests="0" failures="0" time="0">
  </testsuite>
  <testsuite name="@test" timestamp="2019-03-26T11:05:48" tests="1" file="/Users/thanhnguyen/Desktop/ucb-client/test/codeceptjs/tests/smokeCheck/test.js" failures="1" time="0.989">
    <testcase name="@test: test" time="0.989" classname="test">
      <system-out>[[ATTACHMENT|/Users/thanhnguyen/Desktop/ucb-client/test/codeceptjs/output/test_1553598349.failed.png]]</system-out>
      <failure message="expected web application to include &quot;sbc&quot;" type="Error"><![CDATA[expected web application to include "sbc"

Scenario Steps:

- I.see("sbc") at Test.Scenario (test/codeceptjs/tests/smokeCheck/test.js:5:7)
- I.amOnPage("https://google.com") at Test.Scenario (test/codeceptjs/tests/smokeCheck/test.js:4:7)

]]></failure>
    </testcase>
  </testsuite>
</testsuites>

kobenguyent avatar Mar 26 '19 11:03 kobenguyent

The PR is merged for mocha-junit-reporter. Is there a possibility that this feature request gets in?

yogainalift avatar Apr 10 '19 13:04 yogainalift

Hi again!

Thanks for adding this feature!

I have finally tried it just recently and it didn't work for me at first. Then I figured out that adding attachments: true line is insufficient and it also needs to be duplicated in the options object, like this:

mocha-junit-reporter: {
   stdout: "./output/console.log",
   options: {
      mochaFile: "./output/result.xml",
      attachments: true //<-- also here
   },
   attachments: true //<-- here as described
}

With both lines it works as expected. Probably you need to do something about it :)

iljao avatar May 20 '19 19:05 iljao