How to implement 'if' in Gherkin

2019-05-31 09:59发布

I am trying to convert Selenium test to Gherkin. Is there way to implement if statements in Gherkin?

Example : assume the code is written in the below format. I am just writing description as below. Please understand the part after double slash is the actual Selenium code:

// launch the application 
// login to application
// navigate to page
String str;
if(str== "XYZ")
{
    // verify title
}
//verify text field 1
//verify test field 2
//verify select box

For this I am trying to write code in Gherkin as follows

Given user launches the application
When user login with valid credentials
and navigate to required page
When String str is "XYZ"
Then verify title
And verify text field 1
And verify test field 2
And verify select box

but this code is incorrect because if the str is not equal to "XYZ" we want that title should not be verified but other verification like text field1,2 and select box should be verified.

5条回答
Summer. ? 凉城
2楼-- · 2019-05-31 10:24

You can write the scenario, somewhat like this:

Given the user launches the application
When user login with valid credentials
And navigates to required page
Then he should see the page datails

Inside the Then step you manage all the logic.

Then(/^he should see the page details$/) do
  if condition
    ...
  else
    ...
  end
end
查看更多
Ridiculous、
3楼-- · 2019-05-31 10:35

Gherkin is not a programming language to use if or else conditions. It is a part of BDD framework, that is implemented, to make the stakeholders and other non technical resources understand what the test process is about. Hence, it is always recommended, you keep the gherkin as simple and as generic as possible.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-05-31 10:41

Strictly speaking you should create an alternative statement along the lines of:

Given user launches the application When user login with valid credentials and navigate to required page When String str is NOT "XYZ"

查看更多
太酷不给撩
5楼-- · 2019-05-31 10:42

Ideally, this level of detail would not be in your Gherkin scenario. The best approach is describe business use cases, not low level details. This is what Gherkin is designed for: communicating with non-technical stakeholders so that you can work out if you are building the right thing in the first place. Here is what I would write:

Given the user is logged in
And the user is on the required page
When they enter data that requires the optional fields to be validated
And they enter invalid data in the optional fields
Then the form shows an error on the optional fields

The low level details don't matter (that the string is specifically "XYZ" or that it is the title field is not important), so these should be hidden in the step definition and/or unit tests.

In order to continue to check the other fields, you can just add another step after this:

When they enter invalid data in all of the other fields
Then each other field has an error message attached to it.

Again, there is no need to specify the actual fields, or separate them into their own steps. The idea is to express the high level business value of the scenario, i.e. that the form is validated when it should be.

The advantage to keeping things high level is that when the form changes (as it eventually probably will), then this scenario can remain untouched. Which is correct as the business case is the same: it should validate when it's supposed to. All the changes will be in the step definitions. This means that there is no reason to have another discussion with your stakeholders about whether your scenarios are still testing the right thing.

查看更多
Melony?
6楼-- · 2019-05-31 10:46

You don't implement if in Gherkin.

Gherkin is about communication and those you want to communicate with, non coders, don't know what an if statement is. They don't care either.

The solution? Two scenarios to cover both cases.

查看更多
登录 后发表回答