Regex 延长黄瓜步
我有一个黄瓜步骤,看起来像这样:Regex 延长黄瓜步,regex,automated-tests,cucumber,bdd,Regex,Automated Tests,Cucumber,Bdd,我有一个黄瓜步骤,看起来像这样: 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 corre
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
}
但是我的第二步(“我为用户输入正确的凭据”)与第一步定义匹配,只是在凭据中添加了“correct”一词
匹配。第一条规则应更改为
@When("I enter the (\\S+) for the user$")
此处,\S+
匹配1个或多个非空白字符。如果没有非空白字符,请使用\S*
要匹配两个“单词”,您可以使用
@When("I enter the (\\S+\\s+\\S+) for the user$")
请注意,您可以使用量词控制“单词”的数量,例如,这将匹配2或3个单词:
@When("I enter the (\\S+(?:\\s+\\S+){1,2}) for the user$")
要匹配两个或多个单词:
@When("I enter the (\\S+(?:\\s+\\S+){1,}) for the user$")
@When("I enter the (\\S+(?:\\s+\\S+)+) for the user$")
有几种方法可以改进这些步骤并避免使用正则表达式 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的所有复杂性,并将其放在您为创建用户而调用的helper方法中
2) 为步骤定义提供更多参数
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被命名之前,我就一直在反刍,现在我在反刍的时候从来没有使用过regex或场景大纲。您也不需要这样做。一些答案建议使用命令式方法,这被认为是BDD中的一种反模式。相反,我强烈建议您使用自然语言或商业语言,使用声明式方法处理小黄瓜。如果您实际正在测试登录功能,我建议如下:
When an authorised user enters their credentials
@admin, administrator, password
@authorised, user, password
@unauthorised, user, wrong
还是基于角色
When an Administrator is authorised
如果登录实际上是受测功能的先决条件,则可以执行以下操作:
Given an authorised user
或
可以使用凭据管理器备份这些内容
... = ExpectedData.credentialsFor("@authorised");
标签应表示从包含以下内容的测试数据db或csv检索的特征,而不是预期数据的标识:
When an authorised user enters their credentials
@admin, administrator, password
@authorised, user, password
@unauthorised, user, wrong
所有测试数据输入应使用相同的方法,例如:
Given a Cash Customer
Given a Credit Customer
Given a Customer with an overdue account
这种方法的一个很大的好处是,通过使数据/凭证处理程序环境感知,测试套件可以在不同的环境中很容易地重用 尝试
@When(“我为用户$输入(\\S*)”
作为第一条规则更改订单。@WiktorStribiżew这行得通!你能解释一下原因吗?并将其作为答案发布,以便我可以接受。添加了带有更多选项的答案。添加的元字符也是如此。它们是如何工作的?问题是它们之间的字符串的开头和结尾是否相同?这对我很有用。请参阅答案说明中添加的屏幕截图。您没有在步骤定义模式中捕获凭据参数。在添加参数后也会工作。请检查在这两种情况下都将捕获文本“凭据”。不应明确提及凭据。re“所有现有的答案都过于迫切,随着时间的推移和更多答案的加入,这些答案很快就会过时。也许是“一些…”或“避免强制性的解决方案,做一些声明性的事情”…@恶魔主义者单独改写了你建议的台词,任何其他反馈都收到了。