Is there a way to add screenshot to JUnit report as attachment?
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 well, I took some times to look at this, there are several things to make this works:
-
The current npm of this repo
https://github.com/michaelleeallen/mocha-junit-reporteris 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 ;) -
We need also to turn it on from
codeceptjsside, 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)];
};
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 "sbc"" 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>
The PR is merged for mocha-junit-reporter. Is there a possibility that this feature request gets in?
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 :)