扩展Cucumber步骤

3

我有一个Cucumber步骤,看起来像这样:

When I enter the credentials for the user

还有另一个说

When I enter the correct credentials for the user

对应的步骤定义如下:

@When("I enter the ([^\"]*) for the user$")
public void stepDefinition(String cred){
    //code
}
@When("I enter the correct ([^\"]*) for the user$")
public void otherStepDefinition(String cred){
    //other code
}

但是我的第二个黄瓜步骤(“我为用户输入正确的凭据”)与第一个步骤定义匹配,只是在凭据中添加了“正确”的单词。

  1. 我该怎么办?
  2. 我对正则表达式不熟悉。是否可能从'When'步骤中排除“正确”部分,以便我可以拥有一个基本步骤,可以使用“正确”部分进行扩展?

2
尝试将@When("I enter the (\\S*) for the user$")作为第一条规则。 - Wiktor Stribiżew
更改订单。 - revo
@WiktorStribiżew 这个方法可行!你能解释一下为什么吗?并把它作为答案发布,这样我就可以接受它了。 - Mate Mrše
添加了一个带有更多选项的答案。 - Wiktor Stribiżew
4个回答

2

第一条规则应该更改为

最初的回答

@When("I enter the (\\S+) for the user$")

这里的\S+匹配一个或多个非空白字符。如果没有非空白字符可以使用\S*
要匹配两个“单词”,您可以使用
@When("I enter the (\\S+\\s+\\S+) for the user$")

请注意,您可以使用量词控制“单词”的数量,例如,这将匹配2个或3个单词:

最初的回答:注意可以使用量词来控制“单词”数量,例如,以下内容将匹配2个或3个单词:
@When("I enter the (\\S+(?:\\s+\\S+){1,2}) for the user$")

匹配2个或更多单词:

最初的回答:

@When("I enter the (\\S+(?:\\s+\\S+){1,}) for the user$")
@When("I enter the (\\S+(?:\\s+\\S+)+) for the user$")

2

有几种方法可以改进这些步骤,避免使用正则表达式。

1)让用户知道自己的凭据,并要求步骤询问用户凭据。

因此,您将拥有:

最初的回答


Given I am a user
  @user = create_user # method creates a user with credentials
end

When `I enter the users credentials` do
  fill_in username: @user.username
  fill_in password: @user.password
end

When `I enter the wrong credentials for the user` do
  fill_in username: @user.username
  fill_in password: @user.bad_password # or perhaps just bad_password
end

这种方法将所有复杂性从cucumber中移除,并将其放置在你调用以创建用户的辅助方法中。
为你的步骤定义添加更多参数。
When 'I enter the credentials user: (\\S+) password: (\\S+) do |username, password|
  fill_in username: username
  fill_in password: password 
end

When 'I enter the bad credentials user: (\\S+) password: (\\S+) do |username, password|
  fill_in username: username
  fill_in password: password 
end

我强烈推荐第一种方法,您应该让功能和场景保持简单,将复杂性下放到代码中。 与Cucumber相比,代码更擅长处理复杂性。

自从Cucumber未命名之前,我就一直在使用它,现在在测试中再也不使用正则表达式或场景大纲了。 您也不需要。

Original Answer翻译成"最初的回答"


2

有几个答案建议使用命令式方法,然而这在BDD中被认为是反模式。相反,我强烈建议您使用自然语言或业务语言来采用声明式方法编写您的Gherkin。如果您确实要测试登录功能,我建议您尝试以下方式:

"最初的回答"

When an authorised user enters their credentials

最初的回答:或基于角色
When an Administrator is authorised

如果登录是测试功能的前提条件,那么可以这样写:

如果登录是测试功能的前提条件,则应采取以下措施:

最初的回答:
Given an authorised user

最初的回答
或者
Given an authorised Administrator

这些可以通过凭据管理器进行备份。

... = ExpectedData.credentialsFor("@authorised");

这个标签应该代表数据的特征,而不是数据本身的标识。可以从测试数据库或者CSV文件中检索到类似以下内容的数据:

Original Answer翻译成:"最初的回答"

@admin, administrator, password
@authorised, user, password
@unauthorised, user, wrong

所有测试数据输入都应使用相同的方法,例如:

Original Answer翻译成"最初的回答"

Given a Cash Customer
Given a Credit Customer
Given a Customer with an overdue account

这种方法的一个重要好处是测试套件可以很容易地在不同的环境中重复使用,只需让数据/凭据处理程序具有环境感知能力即可。"最初的回答"

2
所有现有的答案都太过命令式了,随着时间的推移和更多的答案被添加,这些答案很快就会过时。也许可以使用“一些……”或者“避免使用命令式解决方案,采用声明式方法……”。 - diabolist
1
@diabolist 重新修改了你建议的那些行,欢迎提供其他反馈。 - Martin of Hessle

-1
请执行以下步骤定义,并让我们知道是否有效。
@When("^I enter the ([^\"]*) for the user$")
public void stepDefinition(String cred){
    //code
}
@When("^I enter the correct ([^\"]*) for the user$")
public void otherStepDefinition(String cred){
    //other code
}

无参数

Feature File Steps implementation Output

带参数

enter image description here enter image description here enter image description here

两个元字符(^,$)被称为锚点,因为它们用于将正则表达式的每个端点绑定到它们匹配的字符串的开头和结尾。


1
添加元字符后,情况是相同的。它们是如何工作的?问题可能是它们之间字符串的开头和结尾相同吗? - Mate Mrše
它对我有效。请查看答案描述中添加的屏幕截图。 - TheSociety
你没有在步骤定义模式中捕获凭据参数。 - Grasshopper
添加参数后也可以工作。请检查。 - TheSociety
文本“credentials”应在两种情况下被捕获。不应明确提及凭证。 - Grasshopper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接