【问题标题】:How to run background before each scenario如何在每个场景之前运行后台
【发布时间】:2018-12-13 19:13:40
【问题描述】:

我有一个带有标签的背景测试用例,我需要每次都在多个场景中运行。

例子:

有 3 个场景和 1 个背景。简而言之,Background 的行为应该类似于测试的@BeforeMethod

所以我的执行应该是这样的

  1. 背景然后场景 1 (@Dev,@tagteacher1)
  2. 先是背景,然后是场景 2 (@Dev,@tagteacher2)
  3. 再次背景,然后是场景 3 (@Dev,@tagteacher3)
@TestStory
    Feature: Teachers' timesheet need to be filled


      Background: 

      Scenario Outline: Open Webpage
        Given User Open teacher application with given <ENDPOINT> 
        And   Login into application with given <USERNAME> and <PASSWORD>
        And User clicks on teacher submission link

        @DEV
        Examples: 
          | endpoint                       | USERNAME | PASSWORD    |
          | http://teachersheetdev.ggn.com | sdrdev| aknewdev|



        @QA
        Examples: 
          | endpoint                      | USERNAME | PASSWORD    |
          | http://teachersheetqa.ggn.com | sdrqa | aknewdev|


    @tagteacher1
    Scenario1: Open app home page and click the button1
    Given I'm at the teachersheet homepage
    When User clicks Add Task button
    Then User should see the tasks schedule


    @tagteacher2
    Scenario1: Open app home page and click the button2
    Given I'm at the teachersheet homepage
    When User clicks Add Task button
    Then User should see the tasks schedule

    @tagteacher3
    Scenario1: Open app home page and click the button3
    Given I'm at the teachersheet homepage
    When User clicks Add Task button
    Then User should see the tasks schedule



    import org.junit.runner.RunWith;
        import com.optum.synergy.common.ui.controller.WebController;
        import cucumber.api.CucumberOptions;
        import cucumber.api.SnippetType;
        import cucumber.api.junit.Cucumber;

        @RunWith(Cucumber.class)
        @CucumberOptions(
                plugin = { "json:target/test_results/cucumber.json"}, 
                features = { "src/main/resources/ui/features" },
             tags ={"@Dev,@tagteacher"},
                snippets = SnippetType.CAMELCASE

                )

        public class CucumberRunnerTest {

            public static void tearDown(){
                WebController.closeDeviceDriver();
            }
        }

当我想使用 Dev 或 QA 环境运行时如何使用标签?

【问题讨论】:

标签: cucumber cucumber-java


【解决方案1】:

您可以通过在配置中设置您正在使用的网站来更轻松地实现这一点 文件(无论是 dev 还是 qa 站点),然后将用户名和密码移至步骤定义,使用与 QA 或 Dev 相关的用户名和密码。

在此之后,您将能够执行此操作:

Background: 
    Given the user has opened the teachers application
    And they have logged in

@teacher
Scenario: Open app home page and view the task schedule
  Given they are on the teachersheet homepage
  When they start to add a task
  Then they should see the task schedule

@teacher
Scenario: Open app home page and view the task schedule
  Given they are on the teachersheet homepage
  When they start to add a task
  Then they should see the task schedule

如果您需要以不同教师的身份登录,则必须将登录步骤移至场景中,因为它们不会相同,您必须提供登录者的详细信息。

作为旁注,请考虑一下您使用的措辞。如果您正在测试网站的设计,那么单击按钮非常好,但是使用 Cucumber 的主要原因是表达意图 - 描述用户应该如何在网站中移动,而不用担心实现细节。这是为了弥合业务和开发团队之间的沟通鸿沟,这样他们就可以弄清楚正在测试哪些场景。实现细节隐藏了测试的意图。

【讨论】:

  • 我有 2 个环境可以运行,即 Dev 和 QA。如何在运行时在标签的基础上使用示例值? @Kyle Fairns
  • 您可以运行该套件两次,使用说明您正在使用的环境的环境变量,使用更改该环境变量的外部脚本运行。如果您使用的是页面对象模型,您将能够轻松地为每个站点存储一个基本 url,并且只需要页面 url 的结尾部分。
  • 是的,我只使用页面对象模型。如何使用环境变量仅运行 Dev env?还有如何使用标签?
  • 使用它们:stackoverflow.com/questions/531694/… 设置它们取决于您使用的是 Windows 还是基于 unix 的系统(如 Mac 或 Linux)。我假设您在 Windows 上:superuser.com/questions/79612/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 2016-03-05
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
相关资源
最近更新 更多