Do we have any annotation in cucumber where it wil

2019-06-05 13:20发布

@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 ?

2条回答
甜甜的少女心
2楼-- · 2019-06-05 13:59

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.

查看更多
走好不送
3楼-- · 2019-06-05 14:15

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.

查看更多
登录 后发表回答