I have a Cucumber step which I want to perform depending on the statement passed in Cucumber step
verify word does exists in log
clear the logs
or I may pass
verify word does not exists in log
not clear the logs
and Gherkin for this would be
("^verify word does(not|) exists in log$")
("^(|not )clear the logs$")
Can I handle this in Java?
I want to perform action depending up on the key I pass
Here is the solution in Ruby, it might give you an idea how to Updat your step and step def.
Step:
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
I found the solution, I have done it in Java as below
@Then("^verify word does(not|) exists in log$")
public void verifyLogs(String action) {
switch statement
code logic
//or
if(action.contains("not")){
code logic
}
}
Based on your examples, I'm assuming you have two different options ("does" or "does not"). You have several options of capturing this in your step definition, for instance
using capture groups:
@Then("^verify word (does|does not) exist in log$")
using regex:
@Then("^verify word (.*) exist in log$")
using Cucumber expressions :
@Then("verify word {string} exist in log")
Next, you'll have to implement the step definition, to do something different depending on whether or not the String you passed contains "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
}
Alternatively, you could use two different step definitions: @Then("^verify word does exist in log$") public void verifyLogs(String shouldContain) { // call helper method to verify logs do contain the word }
and
@Then("^verify word does not exist in log$")
public void verifyLogs(String shouldContain) {
// call helper method to verify logs **do not** contain the word
}
The upside of the last alternative is that your step definitions will stay very simple / don't contain any logic. The downside ofcourse is you have 2 step definitions, which is not a very big downside imho.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.