forked from webdriverio-community/wdio-teamcity-reporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
226 lines (193 loc) · 6.53 KB
/
index.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'use strict'
const WdioReporter = require('@wdio/reporter').default
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const Readable = require('stream').Readable
const { v4: uuid } = require('uuid')
const shell = require('shelljs')
/**
* @typedef {Object} SuiteStats
* @property {string} type
* @property {string} start
* @property {string} uid
* @property {string} cid
* @property {string} title
* @property {string} fullTitle
* @property {undefined} tags
* @property {Array} tests
* @property {Array} hooks
* @property {Array} suites
* @property {Array} hooksAndTests
*/
/**
* @typedef {Object} TestStats
* @property {string} type
* @property {string} start
* @property {number} _duration
* @property {string} uid
* @property {string} cid
* @property {string} title
* @property {string} fullTitle
* @property {Array} output
* @property {any} argument
* @property {string} state
* @property {string} [end]
*/
class WdioTeamcityReporter extends WdioReporter {
static escape (str) {
if (!str) return ''
return str
.toString()
.replace(/\|/g, '||')
.replace(/\n/g, '|n')
.replace(/\r/g, '|r')
.replace(/\[/g, '|[')
.replace(/\]/g, '|]')
.replace(/\u0085/g, '|x') // next line
.replace(/\u2028/g, '|l') // line separator
.replace(/\u2029/g, '|p') // paragraph separator
.replace(/'/g, '|\'')
}
static bool (value, fallback) {
return typeof value === 'boolean' ? value : fallback
}
static number (value, fallback) {
return typeof value === 'number' ? value : fallback
}
static string (value, fallback) {
return typeof value === 'string' ? value : fallback
}
constructor (reporterOptions) {
const r = WdioTeamcityReporter
const params = {
captureStandardOutput: r.bool(reporterOptions.captureStandardOutput, false),
flowId: r.bool(reporterOptions.flowId, true),
message: r.string(reporterOptions.message, '[title]'),
screenshotPath: r.string(reporterOptions.screenshotPath, 'temp/screenshots/'),
stdout: true,
writeStream: process.stdout
}
const options = Object.assign(reporterOptions, params)
super(options)
const folderPath = path.join(process.cwd(), params.screenshotPath)
shell.mkdir('-p', folderPath)
this.fullScreenshotPath = folderPath
this.currentTestStats = null
this.previousTestUid = null
this.iterator = 0
}
/**
* @param {SuiteStats} suiteStats
*/
onSuiteStart (suiteStats) {
this._m('##teamcity[testSuiteStarted name=\'{name}\' flowId=\'{id}\']', suiteStats)
}
/**
* @param {TestStats} testStats
*/
onTestStart (testStats) {
this.currentTestStats = testStats
this._m('##teamcity[testStarted name=\'{name}\' captureStandardOutput=\'{capture}\' flowId=\'{id}\']', testStats)
}
/**
* @param {TestStats} testStats
*/
onTestEnd (testStats) {
if (testStats.state === 'skipped') {
return
}
this._m('##teamcity[testFinished name=\'{name}\' duration=\'{ms}\' flowId=\'{id}\']', testStats)
}
/**
* @param {TestStats} testStats
*/
onTestFail (testStats) {
const { escape, number } = WdioTeamcityReporter
const specFileRetryAttempts = number(this.runnerStat.config.specFileRetryAttempts, 0)
const specFileRetries = number(this.runnerStat.config.specFileRetries, 0)
const attempt = escape(`${specFileRetryAttempts}/${specFileRetries}`)
if (specFileRetryAttempts === specFileRetries) {
this._m('##teamcity[testFailed name=\'{name}\' message=\'{error}\' details=\'{stack}\' flowId=\'{id}\']', testStats)
} else {
this._m(`##teamcity[message name='{name}' text='attempt ${attempt} failed: {error}' flowId='{id}']`, testStats)
}
}
/**
* @param {TestStats} testStats
*/
onTestSkip (testStats) {
this._m('##teamcity[testIgnored name=\'{name}\' message=\'skipped\' flowId=\'{id}\']', testStats)
}
/**
* @param {SuiteStats} suiteStats
*/
onSuiteEnd (suiteStats) {
this._m('##teamcity[testSuiteFinished name=\'{name}\' flowId=\'{id}\']', suiteStats)
}
onAfterCommand (command) {
const screenshotRegEx = /\/session\/[^/]*\/screenshot/
// If this is a screenshot command and there is an associated value (the screenshot data)
if (screenshotRegEx.test(command.endpoint) && command.result.value) {
// Save screenshot to path (TODO: allow path to be defined in initial config)
if (this.currentTestStats.uid === this.previousTestUid) {
this.iterator++
} else {
this.iterator = 0
}
const uuidFilename = uuid()
const bufferData = Buffer.from(command.result.value, 'base64')
const streamData = new Readable()
const fileName = `${this.iterator}-${uuidFilename}.png`
const filePath = path.join(this.fullScreenshotPath, fileName)
streamData.push(bufferData)
streamData.push(null)
streamData.pipe(fs.createWriteStream(filePath))
const screenshotDisplayName = `Screenshot ${this.iterator + 1}`
this._m(`##teamcity[testMetadata name='${screenshotDisplayName}' type='image' value='${fileName}' flowId='{id}']`, this.currentTestStats)
this.previousTestUid = this.currentTestStats.uid
}
}
/**
* @param {string} template
* @param {TestStats | SuiteStats} stats
*/
_m (template, stats) {
assert(stats != null, '_m(): missing stats argument')
if (!this.options.flowId) {
template = template.replace(' flowId=\'{id}\'', '')
}
const fragment = pattern => {
switch (pattern) {
case '{capture}':
return this.options.captureStandardOutput ? 'true' : 'false'
case '{id}':
return this.runnerStat.sessionId + '/' + stats.cid
case '{ms}':
return stats._duration
case '{name}':
var name = this.options.message
if (name.includes('[browser]')) name = name.replace(/\[browser\]/g, this._v())
if (name.includes('[title]')) name = name.replace(/\[title\]/g, stats.title)
return name
case '{state}':
return stats.state
case '{error}':
return stats.error.message
case '{stack}':
return stats.error.stack
default:
return ''
}
}
const m = template.replace(/\{[a-z]+\}/gi, m =>
WdioTeamcityReporter.escape(fragment(m)))
this.write(m + '\n')
}
_v () {
const { browserName, version } = this.runnerStat.capabilities
return `${browserName} ${version}`
}
}
module.exports.default = WdioTeamcityReporter
module.exports.reporterName = 'teamcity'