如何在Java

时间:2019-05-30 14:18:53

标签: java cucumber

我要执行一个黄瓜步操作,具体取决于黄瓜步中传递的语句

verify word does exists in log
clear the logs

或者我可以通过

verify word does not exists in log
not clear the logs

为此,杰金会

("^verify word does(not|) exists in log$")
("^(|not )clear the logs$")

我可以用Java处理吗?

我想根据我通过的键执行操作

3 个答案:

答案 0 :(得分:0)

这是Ruby中的解决方案,它可能会让您了解如何更新步长和步长定义。

步骤:

And verify word does exist in log
And verify word does not exist in log

Step Def:

And(/^verify word does( not)? exist in log$/) do |negative|
    if negative
        # negative logic
    else
        # positive logic
    end
end

答案 1 :(得分:0)

我已经按照以下方法完成了Java

 @Then("^verify word does(not|) exists in log$")
    public void verifyLogs(String action) {

        switch statement
        code logic

//or
        if(action.contains("not")){
            code logic
        }
}

答案 2 :(得分:0)

根据您的示例,我假设您有两个不同的选项(“有”或“没有”)。 您可以在步骤定义中选择多种捕获方式,例如

使用捕获组:

@Then("^verify word (does|does not) exist in log$")

使用正则表达式:

@Then("^verify word (.*) exist in log$")

使用Cucumber expressions

@Then("verify word {string} exist in log")

接下来,您将必须实现步骤定义,根据传递的字符串是否包含“ not”来做不同的事情。

public void verifyLogs(String shouldContain) {
        if(action.contains("not")){
            // call helper method to verify logs **do not** contain the word
        }
        // call helper method to verify logs **do** contain the word
}

或者,您可以使用两个不同的步骤定义:     @Then(“ ^ verify单词确实在log $中存在”)     公共无效verifyLogs(String应该包含){             //调用帮助程序方法以验证日志 do 包含单词     }

@Then("^verify word does not exist in log$")
public void verifyLogs(String shouldContain) {
        // call helper method to verify logs **do not** contain the word
}

最后一种选择的好处是您的步骤定义将保持非常简单/不包含任何逻辑。当然,缺点是您有2个步骤定义,这不是很大的缺点。