Skip to content

Commit 25448ca

Browse files
author
DavertMik
committed
Merge branch '3.x' of github.com:codeceptjs/CodeceptJS into 3.x
2 parents 0c7386a + 171da34 commit 25448ca

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

lib/mocha/test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,10 @@ function cloneTest(test) {
133133
return deserializeTest(serializeTest(test))
134134
}
135135

136-
function testToFileName(test) {
137-
let fileName = clearString(test.title)
136+
function testToFileName(test, suffix = '') {
137+
let fileName = test.title
138+
139+
if (suffix) fileName = `${fileName}_${suffix}`
138140
// remove tags with empty string (disable for now)
139141
// fileName = fileName.replace(/\@\w+/g, '')
140142
fileName = fileName.slice(0, 100)
@@ -146,6 +148,7 @@ function testToFileName(test) {
146148
// if (test.parent && test.parent.title) {
147149
// fileName = `${clearString(test.parent.title)}_${fileName}`
148150
// }
151+
fileName = clearString(fileName).slice(0, 100)
149152
return fileName
150153
}
151154

lib/plugin/screenshotOnFail.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,12 @@ module.exports = function (config) {
8383
async () => {
8484
const dataType = 'image/png'
8585
// This prevents data driven to be included in the failed screenshot file name
86-
let fileName = testToFileName(test)
86+
let fileName
8787

8888
if (options.uniqueScreenshotNames && test) {
89-
const uuid = _getUUID(test)
90-
fileName = `${fileName.substring(0, 10)}_${uuid}.failed.png`
89+
fileName = `${testToFileName(test, _getUUID(test))}.failed.png`
9190
} else {
92-
fileName += '.failed.png'
91+
fileName = `${testToFileName(test)}.failed.png`
9392
}
9493
output.plugin('screenshotOnFail', 'Test failed, try to save a screenshot')
9594

lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function deepMerge(target, source) {
1313
}
1414

1515
module.exports.genTestId = test => {
16-
return require('crypto').createHash('sha256').update(test.fullTitle()).digest('base64').slice(0, -2)
16+
return this.clearString(require('crypto').createHash('sha256').update(test.fullTitle()).digest('base64').slice(0, -2))
1717
}
1818

1919
module.exports.deepMerge = deepMerge

test/unit/plugin/screenshotOnFail_test.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const screenshotOnFail = require('../../../lib/plugin/screenshotOnFail')
88
const container = require('../../../lib/container')
99
const event = require('../../../lib/event')
1010
const recorder = require('../../../lib/recorder')
11-
11+
const { createTest } = require('../../../lib/mocha/test')
12+
const { deserializeSuite } = require('../../../lib/mocha/suite')
1213
let screenshotSaved
1314

1415
describe('screenshotOnFail', () => {
@@ -25,23 +26,23 @@ describe('screenshotOnFail', () => {
2526

2627
it('should remove the . at the end of test title', async () => {
2728
screenshotOnFail({})
28-
event.dispatcher.emit(event.test.failed, { title: 'test title.' })
29+
event.dispatcher.emit(event.test.failed, createTest('test title.'))
2930
await recorder.promise()
3031
expect(screenshotSaved.called).is.ok
3132
expect('test_title.failed.png').is.equal(screenshotSaved.getCall(0).args[0])
3233
})
3334

3435
it('should exclude the data driven in failed screenshot file name', async () => {
3536
screenshotOnFail({})
36-
event.dispatcher.emit(event.test.failed, { title: 'Scenario with data driven | {"login":"admin","password":"123456"}' })
37+
event.dispatcher.emit(event.test.failed, createTest('Scenario with data driven | {"login":"admin","password":"123456"}'))
3738
await recorder.promise()
3839
expect(screenshotSaved.called).is.ok
3940
expect('Scenario_with_data_driven.failed.png').is.equal(screenshotSaved.getCall(0).args[0])
4041
})
4142

4243
it('should create screenshot on fail', async () => {
4344
screenshotOnFail({})
44-
event.dispatcher.emit(event.test.failed, { title: 'test1' })
45+
event.dispatcher.emit(event.test.failed, createTest('test1'))
4546
await recorder.promise()
4647
expect(screenshotSaved.called).is.ok
4748
expect('test1.failed.png').is.equal(screenshotSaved.getCall(0).args[0])
@@ -50,17 +51,34 @@ describe('screenshotOnFail', () => {
5051
it('should create screenshot with unique name', async () => {
5152
screenshotOnFail({ uniqueScreenshotNames: true })
5253

53-
const test = { title: 'test1', uid: 1 }
54+
const test = createTest('test1')
55+
const suite = deserializeSuite({ title: 'suite1' })
56+
test.addToSuite(suite)
57+
5458
event.dispatcher.emit(event.test.failed, test)
5559
await recorder.promise()
5660
expect(screenshotSaved.called).is.ok
61+
expect(screenshotSaved.getCall(0).args[0]).not.to.include('/')
5762
expect(`test1_${test.uid}.failed.png`).is.equal(screenshotSaved.getCall(0).args[0])
5863
})
5964

6065
it('should create screenshot with unique name when uid is null', async () => {
6166
screenshotOnFail({ uniqueScreenshotNames: true })
6267

63-
event.dispatcher.emit(event.test.failed, { title: 'test1' })
68+
const test = createTest('test1')
69+
event.dispatcher.emit(event.test.failed, test)
70+
await recorder.promise()
71+
expect(screenshotSaved.called).is.ok
72+
const fileName = screenshotSaved.getCall(0).args[0]
73+
const regexpFileName = /test1_[0-9]{10}.failed.png/
74+
expect(fileName.match(regexpFileName).length).is.equal(1)
75+
})
76+
77+
it('should create screenshot with unique name when uid is null', async () => {
78+
screenshotOnFail({ uniqueScreenshotNames: true })
79+
80+
const test = createTest('test1')
81+
event.dispatcher.emit(event.test.failed, test)
6482
await recorder.promise()
6583
expect(screenshotSaved.called).is.ok
6684
const fileName = screenshotSaved.getCall(0).args[0]
@@ -70,14 +88,16 @@ describe('screenshotOnFail', () => {
7088

7189
it('should not save screenshot in BeforeSuite', async () => {
7290
screenshotOnFail({ uniqueScreenshotNames: true })
73-
event.dispatcher.emit(event.test.failed, { title: 'test1' }, null, 'BeforeSuite')
91+
const test = createTest('test1')
92+
event.dispatcher.emit(event.test.failed, test, null, 'BeforeSuite')
7493
await recorder.promise()
7594
expect(!screenshotSaved.called).is.ok
7695
})
7796

7897
it('should not save screenshot in AfterSuite', async () => {
7998
screenshotOnFail({ uniqueScreenshotNames: true })
80-
event.dispatcher.emit(event.test.failed, { title: 'test1' }, null, 'AfterSuite')
99+
const test = createTest('test1')
100+
event.dispatcher.emit(event.test.failed, test, null, 'AfterSuite')
81101
await recorder.promise()
82102
expect(!screenshotSaved.called).is.ok
83103
})

0 commit comments

Comments
 (0)