diff --git a/lib/mocha/test.js b/lib/mocha/test.js index a10472f6b..ffe950711 100644 --- a/lib/mocha/test.js +++ b/lib/mocha/test.js @@ -133,8 +133,10 @@ function cloneTest(test) { return deserializeTest(serializeTest(test)) } -function testToFileName(test) { - let fileName = clearString(test.title) +function testToFileName(test, suffix = '') { + let fileName = test.title + + if (suffix) fileName = `${fileName}_${suffix}` // remove tags with empty string (disable for now) // fileName = fileName.replace(/\@\w+/g, '') fileName = fileName.slice(0, 100) @@ -146,6 +148,7 @@ function testToFileName(test) { // if (test.parent && test.parent.title) { // fileName = `${clearString(test.parent.title)}_${fileName}` // } + fileName = clearString(fileName).slice(0, 100) return fileName } diff --git a/lib/plugin/screenshotOnFail.js b/lib/plugin/screenshotOnFail.js index 1e4317317..aeba20834 100644 --- a/lib/plugin/screenshotOnFail.js +++ b/lib/plugin/screenshotOnFail.js @@ -83,13 +83,12 @@ module.exports = function (config) { async () => { const dataType = 'image/png' // This prevents data driven to be included in the failed screenshot file name - let fileName = testToFileName(test) + let fileName if (options.uniqueScreenshotNames && test) { - const uuid = _getUUID(test) - fileName = `${fileName.substring(0, 10)}_${uuid}.failed.png` + fileName = `${testToFileName(test, _getUUID(test))}.failed.png` } else { - fileName += '.failed.png' + fileName = `${testToFileName(test)}.failed.png` } output.plugin('screenshotOnFail', 'Test failed, try to save a screenshot') diff --git a/lib/utils.js b/lib/utils.js index 3696678d9..f27c36e3e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -13,7 +13,7 @@ function deepMerge(target, source) { } module.exports.genTestId = test => { - return require('crypto').createHash('sha256').update(test.fullTitle()).digest('base64').slice(0, -2) + return this.clearString(require('crypto').createHash('sha256').update(test.fullTitle()).digest('base64').slice(0, -2)) } module.exports.deepMerge = deepMerge diff --git a/test/unit/plugin/screenshotOnFail_test.js b/test/unit/plugin/screenshotOnFail_test.js index 4b88d84f7..1cda0ec3f 100644 --- a/test/unit/plugin/screenshotOnFail_test.js +++ b/test/unit/plugin/screenshotOnFail_test.js @@ -8,7 +8,8 @@ const screenshotOnFail = require('../../../lib/plugin/screenshotOnFail') const container = require('../../../lib/container') const event = require('../../../lib/event') const recorder = require('../../../lib/recorder') - +const { createTest } = require('../../../lib/mocha/test') +const { deserializeSuite } = require('../../../lib/mocha/suite') let screenshotSaved describe('screenshotOnFail', () => { @@ -25,7 +26,7 @@ describe('screenshotOnFail', () => { it('should remove the . at the end of test title', async () => { screenshotOnFail({}) - event.dispatcher.emit(event.test.failed, { title: 'test title.' }) + event.dispatcher.emit(event.test.failed, createTest('test title.')) await recorder.promise() expect(screenshotSaved.called).is.ok expect('test_title.failed.png').is.equal(screenshotSaved.getCall(0).args[0]) @@ -33,7 +34,7 @@ describe('screenshotOnFail', () => { it('should exclude the data driven in failed screenshot file name', async () => { screenshotOnFail({}) - event.dispatcher.emit(event.test.failed, { title: 'Scenario with data driven | {"login":"admin","password":"123456"}' }) + event.dispatcher.emit(event.test.failed, createTest('Scenario with data driven | {"login":"admin","password":"123456"}')) await recorder.promise() expect(screenshotSaved.called).is.ok expect('Scenario_with_data_driven.failed.png').is.equal(screenshotSaved.getCall(0).args[0]) @@ -41,7 +42,7 @@ describe('screenshotOnFail', () => { it('should create screenshot on fail', async () => { screenshotOnFail({}) - event.dispatcher.emit(event.test.failed, { title: 'test1' }) + event.dispatcher.emit(event.test.failed, createTest('test1')) await recorder.promise() expect(screenshotSaved.called).is.ok expect('test1.failed.png').is.equal(screenshotSaved.getCall(0).args[0]) @@ -50,17 +51,34 @@ describe('screenshotOnFail', () => { it('should create screenshot with unique name', async () => { screenshotOnFail({ uniqueScreenshotNames: true }) - const test = { title: 'test1', uid: 1 } + const test = createTest('test1') + const suite = deserializeSuite({ title: 'suite1' }) + test.addToSuite(suite) + event.dispatcher.emit(event.test.failed, test) await recorder.promise() expect(screenshotSaved.called).is.ok + expect(screenshotSaved.getCall(0).args[0]).not.to.include('/') expect(`test1_${test.uid}.failed.png`).is.equal(screenshotSaved.getCall(0).args[0]) }) it('should create screenshot with unique name when uid is null', async () => { screenshotOnFail({ uniqueScreenshotNames: true }) - event.dispatcher.emit(event.test.failed, { title: 'test1' }) + const test = createTest('test1') + event.dispatcher.emit(event.test.failed, test) + await recorder.promise() + expect(screenshotSaved.called).is.ok + const fileName = screenshotSaved.getCall(0).args[0] + const regexpFileName = /test1_[0-9]{10}.failed.png/ + expect(fileName.match(regexpFileName).length).is.equal(1) + }) + + it('should create screenshot with unique name when uid is null', async () => { + screenshotOnFail({ uniqueScreenshotNames: true }) + + const test = createTest('test1') + event.dispatcher.emit(event.test.failed, test) await recorder.promise() expect(screenshotSaved.called).is.ok const fileName = screenshotSaved.getCall(0).args[0] @@ -70,14 +88,16 @@ describe('screenshotOnFail', () => { it('should not save screenshot in BeforeSuite', async () => { screenshotOnFail({ uniqueScreenshotNames: true }) - event.dispatcher.emit(event.test.failed, { title: 'test1' }, null, 'BeforeSuite') + const test = createTest('test1') + event.dispatcher.emit(event.test.failed, test, null, 'BeforeSuite') await recorder.promise() expect(!screenshotSaved.called).is.ok }) it('should not save screenshot in AfterSuite', async () => { screenshotOnFail({ uniqueScreenshotNames: true }) - event.dispatcher.emit(event.test.failed, { title: 'test1' }, null, 'AfterSuite') + const test = createTest('test1') + event.dispatcher.emit(event.test.failed, test, null, 'AfterSuite') await recorder.promise() expect(!screenshotSaved.called).is.ok })