我有一个看起来像这样的 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
}
但我的第二个 cucumber 步骤(“我为用户输入了正确的凭据”)与第一步定义匹配,只是在凭据中添加了“正确”一词。
- 我该如何解决这个问题?
- 我是正则表达式的新手。是否可以从“何时”步骤中排除“正确”部分,以便我可以有一个可以用“正确”部分“扩展”的基本步骤?
最佳答案
第一条规则应该改为
@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$")
要匹配 2 个或更多单词:
@When("I enter the (\\S+(?:\\s+\\S+){1,}) for the user$")
@When("I enter the (\\S+(?:\\s+\\S+)+) for the user$")
关于regex - 扩展 Cucumber 步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55261680/