Skip to content

Commit

Permalink
fix(cucumber): Support callback style step definitions (fixes #8)
Browse files Browse the repository at this point in the history
Match syncronized steps with the contract cucumber expects. es6 generators accepted by cucumber are still not supported (#9)
  • Loading branch information
Fialkovskyi, Ievgen (ITCDEC) - KLM authored and Fialkovskyi, Ievgen (ITCDEC) - KLM committed Dec 2, 2016
1 parent f3d7198 commit 4278968
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -35,6 +35,7 @@
},
"dependencies": {
"graceful-fs": "4.1.4",
"is-generator": "1.0.3",
"lodash": "4.13.1",
"mkdirp": "0.5.1",
"moment": "2.14.1",
Expand Down
23 changes: 21 additions & 2 deletions src/serenity-cucumber/webdriver_synchroniser.ts
@@ -1,6 +1,7 @@
import { Deferred } from '../serenity/recording/async';
import withArityOf = require('util-arity');
import { StepDefinitions } from 'cucumber';
import {fn as isGeneratorFn} from 'is-generator';

import * as webdriver from 'selenium-webdriver';

Expand All @@ -23,7 +24,7 @@ export function synchronise (cucumber: StepDefinitions, controlFlow: webdriver.p
// ---

/**
* Creates a synchronising StepGenerator, which looks like a regular StepGenerator but with this signifficant
* Creates a synchronising StepGenerator, which looks like a regular StepGenerator but with this significant
* difference, that any step function passed to it will be wrapped and executed in the context of WebDriver
* Control Flow
*
Expand Down Expand Up @@ -59,15 +60,33 @@ export function synchronise (cucumber: StepDefinitions, controlFlow: webdriver.p
let deferred = new Deferred<void>(),
context = this;

if (isGeneratorFn(originalStep)) {
throw 'Synchronizing e6 generator style steps is not supported by serenity-js yet';
}

controlFlow
.execute(() => originalStep.apply(context, args) )
.then(deferred.resolve, deferred.reject);

return deferred.promise;
if (!hasCallbackInterface(originalStep, args)) {
return deferred.promise;
}
};
}
}

/**
* Assumes that step definition has a callback interface if the number of parameters passed by cucumber
* matches its signature
*
* @return boolean
* @param step
* @param params
*/
function hasCallbackInterface(step: SomeFunction, params: any[]): boolean {
return step.length === params.length;
}

/**
* Makes the pretender function of the same arity as the original one to deceive cucumber.
*
Expand Down

0 comments on commit 4278968

Please sign in to comment.