如何在 Gherkin 中实现“if"如何在、Gherkin、quot、if

2023-09-08 00:34:29 作者:热情太够、

我正在尝试将 Selenium 测试转换为 Gherkin.有没有办法在 Gherkin 中实现 if 语句?

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

示例:假设代码以以下格式编写.我只是写如下描述.请理解双斜杠后的部分是实际的 Selenium 代码:​​

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

为此,我尝试在 Gherkin 中编写如下代码

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

但此代码不正确,因为如果 str 不等于XYZ",我们希望不验证标题,但应验证文本字段 1、2 和选择框等其他验证.

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.

推荐答案

你可以写这个场景,有点像这样:

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

Then 步骤中,您管理所有逻辑.

Inside the Then step you manage all the logic.

Then(/^he should see the page details$/) do
  if condition
    ...
  else
    ...
  end
end