Cucumber Expressions: Non Capture Group #131

KyleFairns opened this Issue Mar 10, 2017 · 2 comments


None yet

3 participants

KyleFairns commented Mar 10, 2017 edited


I'm linking to the issue that I created over in the cucumber-js repo here in order to trace back the whole conversation.

There are many cases in our step definitions where we have a varity of repeated groups that don't change the steps functionality, but does do quite a bit for readablilty in the feature files:

  • "must"/"should"
  • optional words like "the", "I" etc that would match Given I fill in "Foo" & And fill in "Bar"
  • lists of interchangable words

Say that you are using these alternatives across a large set of step definitions and you wish to change them all. This is where the problem (and the solution) both appear.

Current way of completing the task

At current, we can do alternatives like so:

Given('{pronoun} have/has {int} child(ren)', (pronoun, childCount) => {
  // Code that does not require the "pronoun" parameter at all

However, this does mean that we have a parameter that is never actually used in the step definition that we have and this is the only way of storing the list to be used in many differing steps.

Possible Solution

Perhaps square brackets could be used for things that don't need to be captured, but still require the transformation.

Given('[pronoun] have/has {int} child(ren)', (childCount) => {
   // Code

When('[pronoun] picks up {stringInDoubleQuotes} from {stringInDoubleQuotes}', (child, place) => {
   // Code

Then('[pronoun] should have {int} child(ren) in [pronoun] care', (childCount) => {
  // Code

Another plus side, is that if the parameter needs to be captured, the only difference would be the brackets that you use in that step definition:

  Then('{pronoun} does something', (pronoun) => {
     if(pronoun == "Jeff"){
        // Do something different for Jeff
     } else {
       // Do your thing

Context & Motivation

We will be working with cucumber on a wide variety of projects in our company and I can guarantee that many of them will require a large number of declaratively written step definitions that require many alternatives that won't be captured for use within the step definitions.

This would make it easier for us to keep track of all of the alternatives that we will be using, and will mean that we wont have to search through the many step definition files for all of the uses..

And I am certain that others in the cucumber community will be in the same situation as I.

@KyleFairns KyleFairns referenced this issue in cucumber/cucumber-js Mar 10, 2017

Non Capture Group Transform #784

mattwynne commented Mar 10, 2017 edited

Couldn't you just use regular string interpolation here?


const pronoun = "He/She"
Given(`${pronoun} have/has {int} child(ren)`, (childCount) => {
  // Code that does not require the "pronoun" parameter at all

In JavaScript and Ruby you could, @matt, but not in Java or C#.

Further, it becomes cumbersome to use this consistently across different stepdef files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment