Cucumber Expressions:如何获得严格的多字替代文本?

Nat*_*son 4 javascript cucumber cucumberjs

我正在尝试将基于 RegExp 的 Cucumber v1 步骤定义转换为基于 Cucumber 表达式的 Cucumber v2.0.0-rc.9 步骤定义。我有几个使用正则表达式的步骤定义,如下所示:

/^I (?:am on|go to) the "([^"]*)" page$/
Run Code Online (Sandbox Code Playgroud)

这样,我可以在我的功能文件中写入以下任一内容:

I am on the "Login" page
I go to the "Home" page
Run Code Online (Sandbox Code Playgroud)

我想切换到 Cucumber 表达式,以便我可以开始使用自定义参数,但我找不到复制(?:am on|go to). 我想出的最好的方法是使用多个替代文本

I am/go on/to the "{captureString}" page
Run Code Online (Sandbox Code Playgroud)

但我不喜欢这种方法的是它允许编写如下无意义的步骤:

I am to the "Login" page
Run Code Online (Sandbox Code Playgroud)

我还尝试使用包含如下字符的单个可选文本:|

I (am on|go to) the "{captureString}" page
Run Code Online (Sandbox Code Playgroud)

但 cucumber-expressions-javascript 故意转义|字符,所以生成的 RegExp 看起来像这样:

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

有没有办法使用 Cucumber 表达式创建单个、多单词的替代文本组?

小智 5

我最终选择了多个可选文本:

I (am on)(go to) the "{captureString}" page 这个解决方案仍然没有我想要的那么严格,因为它会匹配如下字符串:

I the "Login" page

I am ongo to the "Home" page

但我发现 Cucumber 表达式本身比多个替代文本更具可读性。我仍然愿意接受其他关于“正确”方法的建议。

在变体之间添加斜杠“/”将消除触发I am ongo to the "Home" page

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