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

后端 未结 11 2470
清歌不尽
清歌不尽 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:08

    I think the CucumberWithSerenity register a Listener which stores the current Step Name.

    Try this in your Test-Runner:

    //import net.serenitybdd.cucumber.CucumberWithSerenity;
    @RunWith(CucumberWithSerenity.class)
    @CucumberOptions(...
    

    And then in in your Step:

    //import net.thucydides.core.model.TestStep;
    //import net.thucydides.core.steps.StepEventBus;
    if (!StepEventBus.getEventBus().isBaseStepListenerRegistered()) {
        return "Unknown"; // CucumberWithSerenity is required.
    } 
    String currentStepDescr = StepEventBus.getEventBus().getCurrentStep()
        .transform(TestStep::getDescription)
        .get();
    

    Dependency:

    <dependency>
        <groupId>net.serenity-bdd</groupId>
        <artifactId>serenity-core</artifactId>
        <version>${serenity.version}</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-04 01:08

    Are you asking if it is possible to get some logging that indicates that the step When I enter valid credentials is executed?

    If so, the answer is yes.

    Cucumber as such doesn't have a notion of logging so you would have to add your own favorite logging framework. Since Cucumber doesn't know about logging through your favorite log framework, you will have to add a log statement in each step you implement in Java.

    I have never seen the need for logging myself. The execution log from Maven, or whatever build tool you are using, have been sufficient for me for a long time.

    The reports include the steps executed so that case is covered.

    0 讨论(0)
  • 2020-12-04 01:11

    We solved this problem by wrapping the entire step as a parameter into the Step Definition. In other words, the step

    Given I am on my website
    

    translates into

    'Given I am on my website'
    

    And the step definition will actually accept a string parameter that will correspond to the step

        @And("(.*)") //plus something specific to map step
        public void Initialization(String step) throws Exception {
                //do something with step
        }
    
    0 讨论(0)
  • 2020-12-04 01:11

    you could add a step like

    When I log in with the user 'user' and the password 'password'
    

    and repeat this step whenever you need a login

    You have to put the class containing the step definition in a package used by every Runner that will need the login.

    0 讨论(0)
  • 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$)
    
    0 讨论(0)
提交回复
热议问题