diff --git a/reporter/index.js b/reporter/index.js index 2443bcf..6c1a9f7 100644 --- a/reporter/index.js +++ b/reporter/index.js @@ -56,7 +56,10 @@ class CypressAllureReporter { .now( 'task', 'writeAllureResults', - this.reporter.runtime.config, + { + results: this.reporter.runtime.config, + files: this.reporter.files + }, { log: false } ) .catch((e) => allureDebug && console.error(e)); @@ -120,17 +123,12 @@ Cypress.Allure = allureEnabled ? new CypressAllureReporter() : stubbedAllure; Cypress.Screenshot.defaults({ onAfterScreenshot(_, details) { if (allureEnabled) { - cy.task('copyFileToAllure', details.path, { log: false }).then( - (filePath) => { - filePath && - Cypress.Allure.reporter.currentTest.addAttachment( - details.name || - `${details.specName}:${details.takenAt}`, - ContentType.PNG, - filePath - ); - } - ); + Cypress.Allure.reporter.files.push({ + name: details.name || `${details.specName}:${details.takenAt}`, + path: details.path, + type: ContentType.PNG, + testName: Cypress.Allure.reporter.testNameForAttachment + }); } } }); diff --git a/reporter/mocha-allure/AllureInterface.js b/reporter/mocha-allure/AllureInterface.js index 3999831..b822d9d 100644 --- a/reporter/mocha-allure/AllureInterface.js +++ b/reporter/mocha-allure/AllureInterface.js @@ -16,11 +16,12 @@ Allure.prototype.testAttachment = function (name, content, type) { }; Allure.prototype.attachFile = function (name, path, type) { - cy.now('task', 'copyFileToAllure', path, { - log: false - }).then((allurePath) => - this.currentTest.addAttachment(name, type, allurePath) - ); + this.reporter.files.push({ + name: name, + path: path, + type: type, + testName: this.reporter.testNameForAttachment + }); }; Allure.prototype.writeExecutorInfo = function (info) { diff --git a/reporter/mocha-allure/AllureReporter.js b/reporter/mocha-allure/AllureReporter.js index 9467e54..6950c95 100644 --- a/reporter/mocha-allure/AllureReporter.js +++ b/reporter/mocha-allure/AllureReporter.js @@ -16,8 +16,10 @@ module.exports = class AllureReporter { this.suites = []; this.steps = []; this.commands = []; + this.files = []; this.currentChainer = null; this.runningTest = null; + this.previousTestName = null; this.runtime = runtime; this.currentHook = null; this.parentStep = null; @@ -67,9 +69,21 @@ module.exports = class AllureReporter { } set currentTest(test) { + if (this.runningTest) { + this.previousTestName = this.runningTest.info.name; + } this.runningTest = test; } + get testNameForAttachment() { + const cyTest = cy.state().test; + return ( + (cyTest && cyTest.title) || + (this.currentTest && this.currentTest.info.name) || + previousTestName + ); + } + startSuite(suiteName) { if (this.currentSuite) { if (this.currentSuiteIsGlobal) { diff --git a/writer.js b/writer.js index db0cdb5..0b70ff7 100644 --- a/writer.js +++ b/writer.js @@ -11,7 +11,8 @@ function allureWriter(on, config) { config.env.allureResultsPath || 'allure-results'; on('task', { - writeAllureResults: ({ resultsDir, writer }) => { + writeAllureResults: ({ results, files }) => { + const { resultsDir, writer } = results; const { groups, tests, @@ -23,6 +24,32 @@ function allureWriter(on, config) { try { !fs.existsSync(resultsDir) && fs.mkdirSync(resultsDir, { recursive: true }); + files && + files.forEach((file) => { + if (fs.existsSync(file.path)) { + const ext = path.extname(file.path); + const allureFilePath = path.join( + resultsDir, + `${uuid.v4()}-attachment${ext}` + ); + + fs.copyFileSync(file.path, allureFilePath); + + if (fs.existsSync(allureFilePath)) { + const testsForAttachment = tests.filter( + (t) => t.name === file.testName + ); + + testsForAttachment.forEach((test) => { + test.attachments.push({ + name: file.name, + type: file.type, + source: path.basename(allureFilePath) + }); + }); + } + } + }); groups && groups.forEach((group) => { if (group.children.length) { @@ -67,24 +94,6 @@ function allureWriter(on, config) { } finally { return null; } - }, - copyFileToAllure: (filePath) => { - const resultsDir = process.env.allureResultsPath; - if (process.env.allure === 'true') { - !fs.existsSync(resultsDir) && - fs.mkdirSync(resultsDir, { recursive: true }); - const ext = path.extname(filePath); - const allurePath = path.join( - resultsDir, - `${uuid.v4()}-attachment${ext}` - ); - - fs.copyFileSync(filePath, allurePath); - - return fs.existsSync(allurePath) - ? path.basename(allurePath) - : null; - } } }); }