Skip to content

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

Closed

Description

@iljao

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

Activity

  1. kobenguyent commented on Mar 26, 2019

    @kobenguyent
    Collaborator

    @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 -> michaelleeallen/mocha-junit-reporter@36ea8b8#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)];
            };
  2. kobenguyent commented on Mar 26, 2019

    @kobenguyent
    Collaborator

    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>
  3. yogainalift commented on Apr 10, 2019

    @yogainalift

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

  4. iljao commented on May 20, 2019

    @iljao
    Author

    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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions