在特性文件(Gherkin语言)中是否有if-else概念?

8

有没有办法在功能文件中使用if/else概念?例如:

  Scenario: User should be able to check login page
  Given I am on login page
  When I click on SignIn button 
  Then I should be in home page 
  If yes
  Then I will create a new profile
  Else
  Then I will logout from page 

1
不,根据你的例子,你正在描述两种情况。它们应该分开来。 - jmccure
5个回答

10

我不知道有没有。Gherkin (和cucumber) 最好用于指定离散的业务场景,并且应该是可重复的,否则它们将变得难以理解和测试。看起来你至少有两个故事在这里:

Scenario: A new user should be asked to sign in
  Given I am a new user
  And I navigate to the login page
  When I click on SignIn button
  I should not be able to get to the home page

Scenario: An existing user should be able to log in
  Given I am an existing user
  And I navigate to the login page
  And I submit valid credentials
  When I click on SignIn button
  I should be taken to the home page

8
不可以也不应该这样做。特性文件是用于描述业务行为,而不是编程。
从你的场景中我认为你想要处理不同的行为,根据你是否已注册。为了实现这个目的,你应该编写两个场景。
Given I am registered
When I 
Then I should ....

Given I am a new user
When I ...
Then I should be asked to register

注意这些场景描述并不说明‘如何’完成。在功能中出现像‘我点击foo’的任何内容都是有问题的,应该避免使用。

2
您希望将您的情况如下所示进行调整
Scenario: User should be able to check login page
  Given I am on login page
  When I click on SignIn button 
  Then I should be in home page 
  And check if home page exist and store result as existence
  If existence is true
  Then I will create a new profile
  And Else
  Then I will logout from page 

在“检查首页是否存在并将结果存储为existence”的步骤定义中,通过检查“driver.findElement”是否返回null来检查首页是否存在,并基于此将其存储为true或false,使用键“existence”将其存储在线程上下文实例中。
在“如果existence为true”的步骤定义中,使用键“existence”检索线程上下文实例数据,如果其为true或false,则进行断言,并使用键“conditionresult”将其存储在线程上下文实例中。
在“然后我将创建一个新配置文件”的步骤定义之前,检索键为“conditionresult”的线程上下文实例数据,并检查值是否为true,然后创建新配置文件,并在条件循环内再次将线程上下文实例设置为null。
在“否则”的步骤定义中,检索键为“conditionresult”的线程上下文实例,并检查其是否为false,如果是,则将线程上下文实例再次设置为true。
在“然后我将从页面注销”的步骤定义中,检索键为“conditionresult”的线程上下文实例,并检查其是否为true,如果是,则执行注销,并在条件循环内再次将线程上下文实例设置为null。

0
如果我们在烟雾测试类型的情况下使用Gherkin,并且需要仅使用UI确保数据库中存在某些内容,该怎么办?
Scenario: I need to create one (and only one) Box before I run the rest of my smoke tests
Given I login as Admin
When I am on the Box list Page
Then the test passes if the Box named "QA SmokeTest" exists
When I click the Add New Box Button
And enter the details for a New Box
And press Save New Box
Then the test passes if the Box named "QA SmokeTest" exists

重复使用相同的Then步骤本质上是一个if-else,它将确保我的Box存在,以便我可以在需要Box的smoke测试套件中运行其他测试。

但这取决于能否在测试运行器中停止场景执行或执行某些类似于以下内容的外部操作:
ScenarioContext.Current["TestPassed"] = true;
然后在每个步骤中
if(ScenarioContext.Current.Get<bool>("TestPassed")) return;


-1

您可以在功能文件中使用参数,并根据传递的参数在代码中实现If else。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接