Cucumber Expressions: How do I have strict, multi-word alternative text?

Viewed 3561

I'm trying to convert my RegExp based Cucumber v1 step definitions into Cucumber Expression based Cucumber v2.0.0-rc.9 step definitions. I have a few step definitions that use regular expressions like the following:

/^I (?:am on|go to) the "([^"]*)" page$/

That way, I can write either of the following in my feature file:

I am on the "Login" page
I go to the "Home" page

I'd like to switch to Cucumber Expressions so that I can start making use of Custom Parameters, but I can't find a great way to duplicate the (?:am on|go to). The best I've come up with is to use multiple alternative texts:

I am/go on/to the "{captureString}" page

What I don't like about that approach though is that it allows for writing nonsensical steps like the following:

I am to the "Login" page

I also tried using a single optional text that contained a | character like so:

I (am on|go to) the "{captureString}" page

but cucumber-expressions-javascript intentionally escapes the | character, so the resulting RegExp looked like this:

/^I (?:am on\|go to)? the "([^"]*)" page$/

Is there any way to have a single, multi-word alternative text group using Cucumber Expressions?

4 Answers

I ended up going with multiple optional texts:

I (am on)(go to) the "{captureString}" page This solution is still not as strict as I would like, as it will match strings like the following:

I the "Login" page

I am ongo to the "Home" page

But I find the Cucumber Expression itself more readable that doing multiple alternative texts. I'm still open to other suggestions for the "right" way to do this.

Adding a slash '/' between variants will eliminate I am ongo to the "Home" page from being triggered:

I (am on)/(go to) the "{captureString}" page

The best option is to use a combination between Optional text and Alternative text.

I (am on)/(go to) the "{captureString}" page

It covers the following cases:

  • I am on the "Login" page
  • I go to the "Home" page
Related