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 1 commit
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 @@ -83,6 +83,8 @@ class AllureReporter extends events.EventEmitter {
allure.startCase(test.title)
const currentTest = allure.getCurrentTest()

attachCucumberArgument(allure, test.argument)

const browserName = test.runner[test.cid].browserName || test.cid
const version = test.runner[test.cid].version || ''
currentTest.addParameter('argument', 'browser', `${browserName}-${version}`)
Expand Down Expand Up @@ -282,6 +284,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)
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"npm-run-all": "~4.1.0",
"rimraf": "^2.6.2",
"server-destroy": "^1.0.1",
"wdio-cucumber-framework": "^2.0.1",
"wdio-cucumber-framework": "^2.2.3",
"wdio-jasmine-framework": "^0.3.3",
"wdio-mocha-framework": "^0.5.13",
"wdio-phantomjs-service": "^0.2.2",
Expand Down
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))
})