在场景执行之前调用所有@Before 和@After 场景方法

All @Before and @After scenario methods are getting called before a scenario execution

我有一个包含一些场景的功能文件,并且我有为所有场景定义的步骤定义(.java 个文件)。在每个步骤定义中,我都有 @Before (beforeScenario) 和 @After (afterScenario) 函数。问题是在执行每个场景之前,所有步骤定义中定义的 @Before@After 方法都会被调用。

有没有一种方法可以实现它,只有在执行场景时,才会执行该场景步骤定义文件中的 @Before@After 场景方法,而不是其他步骤定义中的方法。

这是每个场景之前 运行 之前的默认性质,但是如果您想要执行特定的之前,请按照下面的标记进行操作 您可以使用标签名称如下:

专题文件:

@First 
Scenario: This is First Scenario
 Given this is the first step
 When this is the second step
 Then this is the third step

Hook/step 定义 java 文件:

 @Before("@First")
    public void beforeFirst(){
        System.out.println("This will run only before the First Scenario");
    } 

上面的 运行 仅适用于此场景,您可以在功能文件中的其他场景中使用相同的标签来调用之前的内容:

来源和更多详情:

https://www.toolsqa.com/cucumber/tagged-hooks-in-cucumber/