Do we have any annotation in cucumber where it wil

2019-06-05 13:50发布

问题:

@Before method will run before every scenario. Do we have an annotation where it run before any of the scenarion and an annotation after all the scenarios have been executed ?

回答1:

You can use gerkin with qaf where you can use different TestNG listeners and annotations. In addition to that if you are using webdriver you can get additional driver and element listeners support. For example

package my.test.pkg
public class MyClass{
    @BeforeSuite
    public void beforeSuite() {
       //get executed before suite
    }

    @BeforeTest
    public void beforeTest() {
       //get executed before each xml test

    }
    @BeforeMethod
    public void beforeMethod() {
       //get executed before each test case/scenario

    }
    @BeforeGroups(["p1","p2"])
    public void beforeMethod() {
       //get executed before group

    }
    //same for after methods @afterXXXXX

 }

You need to add class in config file:

<test name="Gherkin-QAF-Test">
   <classes>
      <class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
      <class name="my.test.pkg.Myclass" />
   </classes>
</test>

This example is not using listeners. You also can use different listeners.



回答2:

As pointed in comments cucumber does not have an out-of-box solution for this.

But you can create a before hook to run once only using a static flag.

private static boolean skipFlag = false;

@Before
public void beforeHook() {

    if(!skipFlag) {
        do stuff
        skipFlag=true;
    }
}

Modify the Before hook to run for certain tags etc..

The after hook to run at the end is difficult. Either specifically create a scenario or a final step at the end in which does all the after hook stuff. Or you can write the code in a JVM shutdown hook, though it will run after all feature files are run.