Skip to content

Commit

Permalink
Merge eb5bfe8 into 8ba8e0b
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Muchychka committed Aug 12, 2017
2 parents 8ba8e0b + eb5bfe8 commit ecf6392
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 370 deletions.
75 changes: 33 additions & 42 deletions lib/cucumber-api.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const path = require('path')
const pify = require('pify')
const co = require('co')
const tmp = pify(require('tmp'), { include: ['file'] })
const fs = pify(require('fs'), { include: ['readFile', 'writeFile', 'unlink'] })
const Cucumber = {
Cli: require('cucumber/lib/cli').default,
userCodeRunner: require('cucumber/lib/user_code_runner').default,
TestCaseRunner: require('cucumber/lib/runtime/test_case_runner').default,
Attachment: require('cucumber/lib/runtime/attachment_manager/attachment').default,
statuses: require('cucumber/lib/status').default
}
Expand All @@ -14,62 +15,52 @@ tmp.setGracefulCleanup()
module.exports = class CucumberAPI {
addAttachmentProvider (attachmentProvider) {
this.attachmentProvider = attachmentProvider
this.addStepDefinitionRunner()
this.injectAttachmentHandler()
}

isCucumberListener (fn) {
return (/handle(Step|Features)Result/).test(fn.name) ||
(/handle(Before|After)(Scenario|Feature|Features)/).test(fn.name)
}
* handleAttachments (testCase, stepResult, attach) {
if (
!this.attachmentProvider ||
stepResult.status !== Cucumber.statuses.FAILED ||
stepResult._handledByNightwatchCucumber
) return

isStepDefinitionAsynchronous (stepDefinition, parameters) {
return stepDefinition.length === parameters.length + 1
}
stepResult._handledByNightwatchCucumber = true

* handleAttachments (stepResult) {
try {
const feature = stepResult.step.scenario.feature.name
const scenario = stepResult.step.scenario.name
const feature = path.basename(testCase.uri, path.extname(testCase.uri))
const scenario = testCase.pickle.name
const attachment = yield * this.attachmentProvider(feature, scenario)

if (attachment) {
stepResult.attachments.push(new Cucumber.Attachment({
data: attachment.data,
mimeType: attachment.mimeType
}))
attach(attachment)
}
} catch (err) {
console.error(err)
console.error(err.stack)
}
}

wrapStepDefinition (code, parameters) {
injectAttachmentHandler () {
const self = this
if (this.isCucumberListener(code) && parameters.length && 'attachments' in parameters[0]) {
const stepResult = parameters[0]

if (
this.attachmentProvider &&
stepResult.status === Cucumber.statuses.FAILED &&
!stepResult._handledByNightwatchCucumber
) {
stepResult._handledByNightwatchCucumber = true
return co.wrap(function * () {
yield * self.handleAttachments(stepResult)
code.apply(this, arguments)
})
}
}

return code
}
const originalTestCaseRunnerInvokeStep = Cucumber.TestCaseRunner.prototype.invokeStep
Cucumber.TestCaseRunner.prototype.invokeStep = co.wrap(function * (step, stepDefinition) {
const result = yield originalTestCaseRunnerInvokeStep.apply(this, arguments)

yield * self.handleAttachments(
this.testCase,
result,
({ data, mimeType }) => {
this.emit('test-step-attachment', {
index: this.testStepIndex,
data,
media: {
type: mimeType
}
})
}
)

addStepDefinitionRunner () {
const self = this
const originalUserCodeRunnerRun = Cucumber.userCodeRunner.run
Cucumber.userCodeRunner.run = co.wrap(function * (args) {
args.fn = self.wrapStepDefinition(args.fn, args.argsArray)
return yield originalUserCodeRunnerRun.call(this, args)
return result
})
}

Expand Down
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module.exports = function (providedOptions) {
const options = Object.assign({
cucumberArgs: [
'--require', 'features/step_definitions',
'--format', 'pretty',
'--format', 'json:reports/cucumber.json',
'features'
]
Expand Down

0 comments on commit ecf6392

Please sign in to comment.