regex - 扩展 Cucumber 步骤

标签 regex automated-tests cucumber bdd

我有一个看起来像这样的 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 步骤(“我为用户输入了正确的凭据”)与第一步定义匹配,只是在凭据中添加了“正确”一词。

  1. 我该如何解决这个问题?
  2. 我是正则表达式的新手。是否可以从“何时”步骤中排除“正确”部分,以便我可以有一个可以用“正确”部分“扩展”的基本步骤?

最佳答案

第一条规则应该改为

@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/

相关文章:

正则表达式 - 只包含元音或辅音的匹配世界

c# - 在 Linq 语句中使用 "Match"

automated-tests - Cypress.io : Reading and storing value of form input into a JS variable (or constant)

selenium-webdriver - 如何使用 Robot Framework 查找特定 html 元素的 css 样式属性?

testing - Cypress - 检查全局 afterEach 中的测试失败

ruby-on-rails - cucumber capybara 它是如何工作的

java - .* 究竟是什么?在正则表达式中做什么? ".*?([a-m/]*).*"

java - 如何从文本中删除特定的重复字符?

android - BDD Android UI 测试框架?

angular - TypeScript 和 chai-as-promisied : eventually is an invalid property