Skip to content
This repository has been archived by the owner on Dec 18, 2019. It is now read-only.

Passing cucumber-js argument as attachment. #134

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import process from 'process'
import events from 'events'
import Allure from 'allure-js-commons'
import Step from 'allure-js-commons/beans/step'
import {getTestStatus, isEmpty, PASSED, BROKEN, FAILED} from './utils'
import {attachCucumberArgument, getTestStatus, isEmpty, PASSED, BROKEN, FAILED} from './utils'

const LOGGING_HOOKS = ['"before all" hook', '"after all" hook']
const STEP_STATUSES = [FAILED, PASSED, BROKEN]
Expand Down Expand Up @@ -86,6 +86,8 @@ class AllureReporter extends events.EventEmitter {
allure.startCase(test.title)
const currentTest = allure.getCurrentTest()

attachCucumberArgument(allure, test.argument)

this.addBrowserOrDeviceParameter({ cid: test.cid, runner: test.runner })
currentTest.addParameter('environment-variable', 'capabilities', JSON.stringify(test.runner[test.cid]))
currentTest.addParameter('environment-variable', 'spec files', JSON.stringify(test.specs))
Expand Down Expand Up @@ -281,6 +283,7 @@ class AllureReporter extends events.EventEmitter {

if (test.title) {
allure.startStep(test.title)
attachCucumberArgument(allure, test.argument)
}
}

Expand Down
26 changes: 26 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,29 @@ export function getTestStatus (test, config) {
export function isEmpty (object) {
return !object || Object.keys(object).length === 0
}

export function attachCucumberArgument (allure, argument) {
if (!argument) {
return
}

argument = Array.isArray(argument)
? argument[0]
: argument

let attachmentContent, argumentType

if (typeof argument === 'string') {
argumentType = 'DocString'
attachmentContent = argument
} else if (typeof argument === 'object') {
argumentType = 'DataTable'
attachmentContent = argument.rows.reduce((memo, row) => `${memo}${row.cells.join(' | ')}\n`, '')
} else {
return
}

allure.startStep('Params', Date.now())
allure.addAttachment(argumentType, attachmentContent, 'text/plain')
allure.endStep(PASSED)
}
8 changes: 8 additions & 0 deletions test/fixtures/features/steps/passing-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ Then('I run failing step', () => {
Then('I see steps after as pending', () => {
expect(true).to.be.equal(true)
})

When('Step has DataTable parameter', param => {
expect(param.rawTable).to.be.eql([ [ 'some', 'table' ], [ 'passed', 'into' ] ])
})

When('Step has DocString parameter', param => {
expect(param).to.be.equal('some data here')
})
12 changes: 12 additions & 0 deletions test/fixtures/features/withParam.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: A parametrized step

Cucumber js should pass argument into reporter

Scenario: A parametrized scenario
Given Step has DataTable parameter
|some|table|
|passed|into|
And Step has DocString parameter
"""
some data here
"""
37 changes: 37 additions & 0 deletions test/specs/cucumber-params-attachments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { expect } from 'chai'
import { clean, getResultFiles, getResultFileValue, runCucumber } from '../helper'

describe('Cucumber params attachments', () => {
beforeEach(clean)

const checkFn = results => {
expect(results).to.have.lengthOf(1)

const result = results[0]
const attachments = result('test-case[status="passed"] step[status="passed"] attachment')

expect(attachments).to.have.lengthOf(2)

const dataTable = attachments.eq(0)
const docString = attachments.eq(1)

expect(dataTable.attr('title')).to.be.equal('DataTable')
expect(docString.attr('title')).to.be.equal('DocString')
expect(dataTable.attr('type')).to.be.equal('text/plain')
expect(docString.attr('type')).to.be.equal('text/plain')

const attachmentFileNames = getResultFiles('txt')

const dataTableFile = attachmentFileNames.find(filename => filename === dataTable.attr('source'))
const docStringFile = attachmentFileNames.find(filename => filename === docString.attr('source'))

expect(getResultFileValue(dataTableFile)).to.be.equal(`some | table\npassed | into\n`)
expect(getResultFileValue(docStringFile)).to.be.equal('some data here')
}

it('works with common', () => runCucumber(['withParam']).then(checkFn))

it('works with cucumber', () =>
runCucumber(['withParam'], './test/fixtures/wdio.conf/wdio.conf.cucumber.step.js')
.then(checkFn))
})