首页 > 解决方案 > 如何在另一个功能文件之后运行一个功能文件?

问题描述

我有 2 个功能文件,即userstoryteacher1.featureuserstoryteacher2.feature. 基本上userstoryteacher1.feature有它有 2 个标签@Dev@QA.

我想以下列方式运行功能文件:-

  1. 如果我在 Cucumber 类中通过@Dev,@tagteacher那么它应该选择开发 url 来打开带有凭据的页面。

  2. 如果我在 Cucumber 类中通过@QA,@tagteacher那么它应该选择 qa url 来打开带有凭据的页面。

    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();
        }
    }
    
    ---------------------------------------------------------------------------
    userstoryteacher1.feature file :-
    
    @TestStory
    Feature: Teachers timesheet need to be filled
      I want to use this template for my feature file
    
      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|
    -----------------------------------------------------------------------------
    userstoryteacher2.feature file :-
    
    Feature : I'm at the teachers page
    
    @tagteacher
    Scenario: Open app home page and click the button
    Given I'm at the teachersheet homepage
    When User clicks Add Task button
    Then User should see the tasks schedule
    

标签: cucumbercucumber-java

解决方案


Cucumber 的设计使您无法将场景或功能文件链接在一起。每个场景都应该从一开始就作为一个独立的“测试”运行。

使用功能文件进行编程是一种可怕的反模式。而是将编程向下推到步骤定义层,或者更好地推到步骤定义使用的助手中。

如果你想充分利用 Cucumber,你需要用它来表达正在做什么以及为什么它很重要。从你的例子来看,这似乎都是关于教师填写他们的时间表,所以你的场景应该是这样的

Scenario: Fill in timesheet Given I am a teacher And I am logged in When I fill in my timesheet Then I should see my timesheet has been saved.

您在 Givens 中设置状态,并为您创建的每个场景构建辅助方法,以便将来的场景可以轻松设置状态。例如Given I am a teacher可能是这样的

def 'Given I am a teacher' do
  teacher = create_new_teacher;
  register_teacher(teacher)
  return teacher
end

这是建立在以前的情况下注册新教师的。如果您遵循此模式,您可以使用单个 Given 的简单场景,只需使用单个方法调用即可进行大量设置。这比将几个功能文件链接在一起要好得多!!


推荐阅读