【问题标题】:Tagged hooks are running for every test每次测试都运行标记的钩子
【发布时间】:2019-07-09 15:08:41
【问题描述】:

我的 Hooks 类如下:

@Before("@Firefox")
public void setUpFirefox() {}

@Before("@Chrome")
public void setUpChrome() {}

@After
public void tearDown(){}

当我运行以下命令 mvn test -Dcucumber.options="--tags @Chrome" 时,两个 @Before 函数都在调用。

如何根据 maven 命令运行特定的 @Before 方法?

我的 Runner 课程(我已经尝试过使用 tags 选项,它也不适合我):

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = {"pretty", "json:target/cucumber-reports/Cucumber.json",
                "junit:target/cucumber-reports/Cucumber.xml",
                "html:target/cucumber-reports"},
        features = {"src/test/features/"},
        glue = {"steps"})


public class RunCukesTest {
}

我的功能文件:

Feature: Storybook
  @Test @Widgets @Smoke @Chrome @Firefox
  Scenario: Storybook example
    Given The user clicks on "storybook" index on the homepage
    And Storybook HomePage should be displayed

【问题讨论】:

    标签: maven selenium cucumber cucumber-java cucumber-junit


    【解决方案1】:

    看起来是因为您已经获得了该场景的两个标签,before 挂钩似乎是根据正在运行的场景执行的。例如

    --tag 命令行 --tags @Chrome 等指定运行哪个场景

    -现在基于该场景,使用附加到该场景的标签(Test、Widgets、Smoke、Chrome、Firefox)执行之前的函数

    如果你要为标签 Smoke 设置一个 Before 钩子,我想它也会运行。

    例如:

    (在 Scala 中)

    Before("@test1") { _ =>
        println("test1 before actioned")
      }
    
      Before("@test2") { _ =>
        println("test2 before actioned")
      }
    

    使用功能文件:

    [...]
      @test1
      Scenario: Test scenario 1
        Given Some precondition occurs
    
      @test2
      Scenario: Test scenario 2
        Given Some precondition occurs
    
    

    当我运行这些标签中的任何一个时,我都会得到输出

    test1 before actioned 或 test2 before actioned

    但是,在一种情况下同时使用两个标签,然后打印两行。

    在那些 setupChome、setupFirefox 函数中正在执行什么操作,只是设置驱动程序?您可以创建新的系统属性,例如 browser,匹配值并执行一些设置,然后您可以输入:

    -Dbrowser=chrome 它会以这种方式进行设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      • 2019-03-19
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多