我正在尝试将基于RegExp的Cucumber v1步骤定义转换为基于Cucumber表达式的Cucumber v2.0.0-rc.9步骤定义。我有几个使用正则表达式的步骤定义,如下所示:
/^I (?:am on|go to) the "([^"]*)" page$/
这样,我就可以在我的功能文件中编写以下内容之一:
I am on the "Login" page
I go to the "Home" page
我想切换到黄瓜表达式,这样我就可以开始使用自定义参数,但我找不到复制(?:am on|go to)
的好方法。我想到的最好的办法就是使用多个alternative texts
I am/go on/to the "{captureString}" page
然而,我不喜欢这种方法的是,它允许编写像下面这样的无意义的步骤:
I am to the "Login" page
我还尝试使用包含|
字符的单个optional text,如下所示:
I (am on|go to) the "{captureString}" page
但是cucumber-expressions javascript故意使用escapes the |
character,因此生成的RegExp如下所示:
/^I (?:am on\|go to)? the "([^"]*)" page$/
有没有办法使用Cucumber表达式拥有一个由多个单词组成的替代文本组?
发布于 2019-06-12 14:59:22
我最终使用了多个可选文本:
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
发布于 2017-05-12 18:42:12
我最终选择了多个可选文本:
I (am on)(go to) the "{captureString}" page
这个解决方案仍然不像我希望的那样严格,因为它将像下面这样匹配字符串:
I the "Login" page
I am ongo to the "Home" page
但我发现Cucumber表达式本身比处理多个可选文本更具可读性。对于“正确”的方式,我仍然对其他建议持开放态度。
发布于 2017-05-16 15:09:29
另一种选择是编写您的步骤,以便它们描述您所做的事情,而不是您是如何做到的
Given I am logged in
Give I am a visitor
并且通常避免使用正则表达式或表达式。就我个人而言,我更希望
Given 'I am logged in' do
login user: @i
end
Given 'I have logged in' do
login user: @i
end
而不是只有一个表达式来做这两件事。我的理由是:
我从来没有任何步骤来说明我在哪个页面上,因为这就是我如何做的事情,而场景应该只是关于什么和为什么。
无论如何,希望这会有一些用处。
https://stackoverflow.com/questions/43926907
复制相似问题