In Cucumber, is it possible to programmatically get the current step being executed?

后端 未结 11 2668
清歌不尽
清歌不尽 2020-12-04 00:22
Scenario: As a user, I want to login to the system
Given I am on my website
When I enter valid credentials
Then I am taken to the home page

The sce

11条回答
  •  悲&欢浪女
    2020-12-04 01:12

    Grabbing the annotation using self-reflection seems more straightforward to me:

    import java.lang.annotation.Annotation;
    import java.lang.reflect.Method;
    
    @When("^User enters username and password$")
    public void userEntersUsernameAndPassword() throws Throwable{
        Method callingMethod = new Object() {} .getClass() .getEnclosingMethod();
        Annotation  myAnnotation = callingMethod.getAnnotations()[0];   
        System.out.println("myAnnotation=" + myAnnotation);
    

    Results in:

    myAnnotation=@cucumber.api.java.en.Given(timeout=0, value=^User is in portal page$)
    

提交回复
热议问题