All files / wdio-allure-reporter/src index.js

98.82% Statements 167/169
98.29% Branches 115/117
100% Functions 40/40
98.82% Lines 167/169

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438                50x   50x       50x 50x   50x 50x       50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x       18x 18x       16x 16x 16x       15x       15x   15x   15x 12x 12x 12x 12x 12x 12x   3x       15x 15x 15x       4x       5x 1x   4x     5x 5x       5x       11x 3x   8x         7x 1x     6x 2x     4x   4x 3x         8x 3x     5x 5x 2x 1x     5x 4x 2x     4x 4x       4x           5x 2x       3x 2x 1x   2x       1x         9x 3x       6x 2x 1x   1x   2x       4x 2x   2x     2x 1x           3x 1x     2x 2x       3x 1x     2x 2x       3x 1x     2x 2x       3x 1x     2x 2x       3x 1x     2x 2x       3x 1x     2x 2x       3x 1x     2x 2x       5x 1x     4x 1x   3x         6x 1x   5x       6x 1x   5x       4x 1x   3x 3x 2x   3x       3x 1x   2x 2x       65x       12x 12x       6x               3x 1x               3x 1x               3x 1x               3x 1x               3x 1x                 3x 1x                 3x 1x                   3x 2x               3x 1x               3x 2x 1x   1x                         3x 6x 1x     5x 5x                 3x 1x          
import WDIOReporter from '@wdio/reporter'
import Allure from 'allure-js-commons'
import Step from 'allure-js-commons/beans/step'
import { getTestStatus, isEmpty, tellReporter, isMochaEachHooks, getErrorFromFailedTest } from './utils'
import { events, stepStatuses, testStatuses } from './constants'
 
class AllureReporter extends WDIOReporter {
    constructor(options) {
        const outputDir = options.outputDir || 'allure-results'
 
        super({
            ...options,
            outputDir
        })
        this.config = {}
        this.allure = new Allure()
 
        this.allure.setOptions({ targetDir: outputDir })
        this.registerListeners()
    }
 
    registerListeners() {
        process.on(events.addFeature, ::this.addFeature)
        process.on(events.addStory, ::this.addStory)
        process.on(events.addSeverity, ::this.addSeverity)
        process.on(events.addIssue, ::this.addIssue)
        process.on(events.addTestId, ::this.addTestId)
        process.on(events.addEnvironment, ::this.addEnvironment)
        process.on(events.addAttachment, ::this.addAttachment)
        process.on(events.addDescription, ::this.addDescription)
        process.on(events.startStep, ::this.startStep)
        process.on(events.endStep, ::this.endStep)
        process.on(events.addStep, ::this.addStep)
        process.on(events.addArgument, ::this.addArgument)
    }
 
    onRunnerStart(runner) {
        this.config = runner.config
        this.isMultiremote = runner.isMultiremote || false
    }
 
    onSuiteStart(suite) {
        const currentSuite = this.allure.getCurrentSuite()
        const prefix = currentSuite ? currentSuite.name + ': ' : ''
        this.allure.startSuite(prefix + suite.title)
    }
 
    onSuiteEnd() {
        this.allure.endSuite()
    }
 
    onTestStart(test) {
        this.allure.startCase(test.title)
 
        const currentTest = this.allure.getCurrentTest()
 
        if (!this.isMultiremote) {
            const { browserName, deviceName } = this.config.capabilities
            const targetName = browserName || deviceName || test.cid
            const version = this.config.capabilities.version || this.config.capabilities.platformVersion || ''
            const paramName = deviceName ? 'device' : 'browser'
            const paramValue = version ? `${targetName}-${version}` : targetName
            currentTest.addParameter('argument', paramName, paramValue)
        } else {
            currentTest.addParameter('argument', 'isMultiremote', 'true')
        }
 
        // Allure analytics labels. See https://github.com/allure-framework/allure2/blob/master/Analytics.md
        currentTest.addLabel('language', 'javascript')
        currentTest.addLabel('framework', 'wdio')
        currentTest.addLabel('thread', test.cid)
    }
 
    onTestPass() {
        this.allure.endCase(testStatuses.PASSED)
    }
 
    onTestFail(test) {
        if (!this.isAnyTestRunning()) {
            this.allure.startCase(test.title)
        } else {
            this.allure.getCurrentTest().name = test.title
        }
 
        const status = getTestStatus(test, this.config)
        while (this.allure.getCurrentSuite().currentStep instanceof Step) {
            this.allure.endStep(status)
        }
 
        this.allure.endCase(status, getErrorFromFailedTest(test))
    }
 
    onTestSkip(test) {
        if (!this.allure.getCurrentTest() || this.allure.getCurrentTest().name !== test.title) {
            this.allure.pendingCase(test.title)
        } else {
            this.allure.endCase(testStatuses.PENDING)
        }
    }
 
    onBeforeCommand(command) {
        if (!this.isAnyTestRunning()) {
            return
        }
 
        if (this.options.disableWebdriverStepsReporting || this.isMultiremote) {
            return
        }
 
        this.allure.startStep(`${command.method} ${command.endpoint}`)
 
        if (!isEmpty(command.body)) {
            this.dumpJSON('Request', command.body)
        }
    }
 
    onAfterCommand(command) {
        if (!this.isAnyTestRunning() || this.isMultiremote) {
            return
        }
 
        const { disableWebdriverStepsReporting, disableWebdriverScreenshotsReporting } = this.options
        if (this.isScreenshotCommand(command) && command.result.value) {
            if (!disableWebdriverScreenshotsReporting) {
                this.allure.addAttachment('Screenshot', Buffer.from(command.result.value, 'base64'))
            }
        }
        if (!disableWebdriverStepsReporting) {
            if (command.result && command.result.value && !this.isScreenshotCommand(command)) {
                this.dumpJSON('Response', command.result.value)
            }
 
            const suite = this.allure.getCurrentSuite()
            Iif (!suite || !(suite.currentStep instanceof Step)) {
                return
            }
 
            this.allure.endStep(testStatuses.PASSED)
        }
    }
 
    onHookStart(hook) {
        // ignore global hooks
        if (!hook.parent || !this.allure.getCurrentSuite()) {
            return false
        }
 
        // add beforeEach / afterEach hook as step to test
        if (isMochaEachHooks(hook.title)) {
            if (this.allure.getCurrentTest()) {
                this.allure.startStep(hook.title)
            }
            return
        }
 
        // add hook as test to suite
        this.onTestStart(hook)
    }
 
    onHookEnd(hook) {
        // ignore global hooks
        if (!hook.parent || !this.allure.getCurrentSuite() || !this.allure.getCurrentTest()) {
            return false
        }
 
        // set beforeEach / afterEach hook (step) status
        if (isMochaEachHooks(hook.title)) {
            if (hook.error) {
                this.allure.endStep(stepStatuses.FAILED)
            } else {
                this.allure.endStep(stepStatuses.PASSED)
            }
            return
        }
 
        // set hook (test) status
        if (hook.error) {
            this.onTestFail(hook)
        } else {
            this.onTestPass()
 
            // remove hook from suite if it has no steps
            if (this.allure.getCurrentTest().steps.length === 0) {
                this.allure.getCurrentSuite().testcases.pop()
            }
        }
    }
 
    addStory({ storyName }) {
        if (!this.isAnyTestRunning()) {
            return false
        }
 
        const test = this.allure.getCurrentTest()
        test.addLabel('story', storyName)
    }
 
    addFeature({ featureName }) {
        if (!this.isAnyTestRunning()) {
            return false
        }
 
        const test = this.allure.getCurrentTest()
        test.addLabel('feature', featureName)
    }
 
    addSeverity({ severity }) {
        if (!this.isAnyTestRunning()) {
            return false
        }
 
        const test = this.allure.getCurrentTest()
        test.addLabel('severity', severity)
    }
 
    addIssue({ issue }) {
        if (!this.isAnyTestRunning()) {
            return false
        }
 
        const test = this.allure.getCurrentTest()
        test.addLabel('issue', issue)
    }
 
    addTestId({ testId }) {
        if (!this.isAnyTestRunning()) {
            return false
        }
 
        const test = this.allure.getCurrentTest()
        test.addLabel('testId', testId)
    }
 
    addEnvironment({ name, value }) {
        if (!this.isAnyTestRunning()) {
            return false
        }
 
        const test = this.allure.getCurrentTest()
        test.addParameter('environment-variable', name, value)
    }
 
    addDescription({ description, type }) {
        if (!this.isAnyTestRunning()) {
            return false
        }
 
        const test = this.allure.getCurrentTest()
        test.setDescription(description, type)
    }
 
    addAttachment({ name, content, type = 'text/plain' }) {
        if (!this.isAnyTestRunning()) {
            return false
        }
 
        if (type === 'application/json') {
            this.dumpJSON(name, content)
        } else {
            this.allure.addAttachment(name, Buffer.from(content), type)
        }
    }
 
    startStep(title) {
        if (!this.isAnyTestRunning()) {
            return false
        }
        this.allure.startStep(title)
    }
 
    endStep(status) {
        if (!this.isAnyTestRunning()) {
            return false
        }
        this.allure.endStep(status)
    }
 
    addStep({ step }) {
        if (!this.isAnyTestRunning()) {
            return false
        }
        this.startStep(step.title)
        if (step.attachment) {
            this.addAttachment(step.attachment)
        }
        this.endStep(step.status)
    }
 
    addArgument({ name, value }) {
        if (!this.isAnyTestRunning()) {
            return false
        }
        const test = this.allure.getCurrentTest()
        test.addParameter('argument', name, value)
    }
 
    isAnyTestRunning() {
        return this.allure.getCurrentSuite() && this.allure.getCurrentTest()
    }
 
    isScreenshotCommand(command) {
        const isScrenshotEndpoint = /\/session\/[^/]*\/screenshot/
        return isScrenshotEndpoint.test(command.endpoint)
    }
 
    dumpJSON(name, json) {
        this.allure.addAttachment(name, JSON.stringify(json, null, 2), 'application/json')
    }
 
    /**
     * Assign feature to test
     * @name addFeature
     * @param {(string)} featureName - feature name or an array of names
     */
    static addFeature = (featureName) => {
        tellReporter(events.addFeature, { featureName })
    }
 
    /**
     * Assign severity to test
     * @name addSeverity
     * @param {string} severity - severity value
     */
    static addSeverity = (severity) => {
        tellReporter(events.addSeverity, { severity })
    }
 
    /**
     * Assign issue id to test
     * @name addIssue
     * @param {string} issue - issue id value
     */
    static addIssue = (issue) => {
        tellReporter(events.addIssue, { issue })
    }
 
    /**
     * Assign TMS test id to test
     * @name addTestId
     * @param {string} testId - test id value
     */
    static addTestId = (testId) => {
        tellReporter(events.addTestId, { testId })
    }
 
    /**
     * Assign story to test
     * @name addStory
     * @param {string} storyName - story name for test
     */
    static addStory = (storyName) => {
        tellReporter(events.addStory, { storyName })
    }
 
    /**
     * Add environment value
     * @name addEnvironment
     * @param {string} name - environment name
     * @param {string} value - environment value
     */
    static addEnvironment = (name, value) => {
        tellReporter(events.addEnvironment, { name, value })
    }
 
    /**
     * Assign test description to test
     * @name addDescription
     * @param {string} description - description for test
     * @param {string} type - description type 'text'\'html'\'markdown'
     */
    static addDescription = (description, type) => {
        tellReporter(events.addDescription, { description, type })
    }
 
    /**
     * Add attachment
     * @name addAttachment
     * @param {string} name - attachment file name
     * @param {string | Buffer} content - attachment content
     * @param {string} [type='text/plain'] - attachment mime type
     */
    static addAttachment = (name, content, type = 'text/plain') => {
        tellReporter(events.addAttachment, { name, content, type })
    }
 
    /**
     * Start allure step
     * @name startStep
     * @param {string} title - step name in report
     */
    static startStep = (title) => {
        tellReporter(events.startStep, title)
    }
 
    /**
     * End current allure step
     * @name endStep
     * @param {string} [status='passed'] - step status
     */
    static endStep = (status = stepStatuses.PASSED) => {
        if (!Object.values(stepStatuses).includes(status)) {
            throw new Error(`Step status must be ${Object.values(stepStatuses).join(' or ')}. You tried to set "${status}"`)
        }
        tellReporter(events.endStep, status)
    }
 
    /**
     * Create allure step
     * @name addStep
     * @param {string} title - step name in report
     * @param {Object} [attachmentObject={}] - attachment for step
     * @param {string} attachmentObject.content - attachment content
     * @param {string} [attachmentObject.name='attachment'] - attachment name
     * @param {string} [attachmentObject.type='text/plain'] - attachment type
     * @param {string} [status='passed'] - step status
     */
    static addStep = (title, { content, name = 'attachment', type = 'text/plain' } = {}, status = stepStatuses.PASSED) => {
        if (!Object.values(stepStatuses).includes(status)) {
            throw new Error(`Step status must be ${Object.values(stepStatuses).join(' or ')}. You tried to set "${status}"`)
        }
 
        const step = content ? { title, attachment: { content, name, type }, status } : { title, status }
        tellReporter(events.addStep, { step })
    }
 
    /**
     * Add additional argument to test
     * @name addArgument
     * @param {string} name - argument name
     * @param {string} value - argument value
     */
    static addArgument = (name, value) => {
        tellReporter(events.addArgument, { name, value })
    }
}
 
export default AllureReporter