Cucumber 如何在Java中使用否定表达式

Cucumber 如何在Java中使用否定表达式,cucumber,cucumber-jvm,cucumber-java,Cucumber,Cucumber Jvm,Cucumber Java,在Cucumber中,您可以编写Then表达式及其步骤定义来验证结果。问题是我不想写两个不同的步骤定义来检查结果。例如: Then the transaction is successful 及 我怎样才能避开这件事? 我发现在Ruby中,您可以通过使用所描述的捕获可选组来整合步骤定义。即: Then /^I should( not)? see the following columns: "([^"]*)"$/ do |negate, columns| within('table the

在Cucumber中,您可以编写Then表达式及其步骤定义来验证结果。问题是我不想写两个不同的步骤定义来检查结果。例如:

Then the transaction is successful

我怎样才能避开这件事? 我发现在Ruby中,您可以通过使用所描述的捕获可选组来整合步骤定义。即:

Then /^I should( not)? see the following columns: "([^"]*)"$/ do |negate, columns|
  within('table thead tr') do
    columns.split(', ').each do |column|
      negate ? page.should_not(have_content(column)) : page.should(have_content(column))
    end
  end
end

但我不知道这在Java中是否可行。即使它是什么类型的变量,我应该捕获

为什么不写两步定义呢。每一个都比较简单,在主题上,不需要正则表达式。如果您将步骤定义所做的工作委托给助手方法,那么您也可以删除几乎所有的代码重复

Then I should see the following columns |cols|
  should_see_cols(cols)
end

Then I should not see the following columns |cols|
  should_not_see_cols(cols)
end
现在您有了超级简单、清晰的步骤定义,您可以随心所欲地编写方法


如果您的所有步骤定义只对一个helper方法进行一次调用,那么步骤定义重复是不相关的。使用helper方法时,您可以随心所欲地使用它,并且仍然保持场景超级简单,并且不需要在step defs中使用regex和复杂的逻辑。

在Java中,我将创建一个带有捕获组(is | is not)的方法,并从中导出一个布尔值,然后将其与值进行比较。另一方面,这为您的测试实现添加了逻辑,因此@diabolist的解决方案有两个不同的步骤定义。如果您想将文本捕获到字符串变量中,您也可以对Java使用相同的步骤定义。尽管在运行“not”步骤时会有一个前导空格。如果您不想捕获,请使用此正则表达式-“^the transaction is(?:not)?successful$”我完全同意,使用两个单独的步骤定义可以使实现更干净、更高效。
Then I should see the following columns |cols|
  should_see_cols(cols)
end

Then I should not see the following columns |cols|
  should_not_see_cols(cols)
end