-
-
Notifications
You must be signed in to change notification settings - Fork 752
Expand file tree
/
Copy pathscreenshotOnFail_test.js
More file actions
143 lines (118 loc) · 5.12 KB
/
Copy pathscreenshotOnFail_test.js
File metadata and controls
143 lines (118 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
let expect
import('chai').then(chai => {
expect = chai.expect
})
const sinon = require('sinon')
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')
const MochawesomeHelper = require('../../../lib/helper/Mochawesome')
let screenshotSaved
describe('screenshotOnFail', () => {
beforeEach(() => {
recorder.reset()
screenshotSaved = sinon.spy()
container.clear({
WebDriver: {
options: {},
saveScreenshot: screenshotSaved,
},
})
})
it('should remove the . at the end of test title', async () => {
screenshotOnFail({})
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, 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, createTest('test1'))
await recorder.promise()
expect(screenshotSaved.called).is.ok
expect('test1.failed.png').is.equal(screenshotSaved.getCall(0).args[0])
})
it('should create screenshot with unique name', async () => {
screenshotOnFail({ uniqueScreenshotNames: true })
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 })
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]
const regexpFileName = /test1_[0-9]{10}.failed.png/
expect(fileName.match(regexpFileName).length).is.equal(1)
})
it('should not save screenshot in BeforeSuite', async () => {
screenshotOnFail({ uniqueScreenshotNames: true })
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 })
const test = createTest('test1')
event.dispatcher.emit(event.test.failed, test, null, 'AfterSuite')
await recorder.promise()
expect(!screenshotSaved.called).is.ok
})
it('should have the same unique file name as the mochawesome helper when the uuid is present', async () => {
screenshotOnFail({ uniqueScreenshotNames: true })
const test = createTest('test1')
test.uid = '1234'
const helper = new MochawesomeHelper({ uniqueScreenshotNames: true })
const spy = sinon.spy(helper, '_addContext')
helper._failed(test)
event.dispatcher.emit(event.test.failed, test)
await recorder.promise()
const screenshotFileName = screenshotSaved.getCall(0).args[0]
expect(spy.getCall(0).args[1]).to.equal(screenshotFileName)
})
it('should have the same unique file name as the mochawesome helper when the uuid is not present', async () => {
screenshotOnFail({ uniqueScreenshotNames: true })
const test = createTest('test1')
// Use sinon to stub Date.now to return consistent timestamp
const clock = sinon.useFakeTimers(1755596785000) // Fixed timestamp
const helper = new MochawesomeHelper({ uniqueScreenshotNames: true })
const spy = sinon.spy(helper, '_addContext')
helper._failed(test)
event.dispatcher.emit(event.test.failed, test)
await recorder.promise()
clock.restore()
const screenshotFileName = screenshotSaved.getCall(0).args[0]
expect(spy.getCall(0).args[1]).to.equal(screenshotFileName)
})
// TODO: write more tests for different options
})