Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/mocha/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
7 changes: 3 additions & 4 deletions lib/plugin/screenshotOnFail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 28 additions & 8 deletions test/unit/plugin/screenshotOnFail_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -25,23 +26,23 @@ 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])
})

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])
})

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])
Expand All @@ -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]
Expand All @@ -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
})
Expand Down