serenity-bdd / serenity-cucumber

Cucumber integration for the Serenity BDD Reporting library
Other
78 stars 74 forks source link

Serenity step libraries in a class implementing 'Backend' interface #180

Open qaEng1213 opened 5 years ago

qaEng1213 commented 5 years ago

Hey guys, I have the following issue:

I'm using serenity + cucumber on Java 8. I need a point in the test execution flow where I can execute code single time before tests' start (the so called "@BeforeTestRun hook"). The workaround I've found is this class as suggested here: https://github.com/cucumber/cucumber-jvm/issues/515#issuecomment-408560807

package cucumber.runtime;

import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.snippets.FunctionNameGenerator;
import gherkin.pickles.PickleStep;

import java.util.List;

public class WhateverYouWant implements Backend {
    private Glue glue;
    private List<String> gluePaths;

    public WhateverYouWant(ResourceLoader resourceLoader)  {  }

    @Override
    public void loadGlue(Glue glue, List<String> gluePaths) {
        this.glue = glue;
        this.gluePaths = gluePaths;

        // do your before hooks here
    }

        @Override
    public void disposeWorld() {
        // do your after hooks here
    }

    @Override
    public void setUnreportedStepExecutor(UnreportedStepExecutor executor) { }

    @Override
    public void buildWorld() { }

    @Override
    public String getSnippet(PickleStep pickleStep, String s, FunctionNameGenerator functionNameGenerator) {
        return null;
    }
}

The bigger issue is that I want to use Serenity step libraries in loadGlue or buildWorld method but when I put a private property and annotate it like:

@Steps
private SomeClass someClass;

and then try to invoke a method from the Step Library class, I get a NullPointerException.

Please give any suggestions on how can I have the step libraries instantiated in the above class or where can I inject code which will run before tests execution and Serenity step library classes will be usable ?

Thank you in advance

wakaleo commented 5 years ago

The Backend classes would get called before the @Steps fields are injected (this is the way Cucumber works), but for setup code, I would generally recommend not using @Steps-instrumented classes, since this is setup code that you normally wouldn't want appearing in your test reports.

qaEng1213 commented 5 years ago

@wakaleo special thanks to you for the answer. If we leave aside test reports how could I implement this solution for setup code with @steps fields. I have a lot of complex logic in step libraries which I would like to reuse and "polluting" test reports is not such a big problem for now.

qaEng1213 commented 5 years ago

@wakaleo any solution for the question in my last comment here ?

wakaleo commented 5 years ago

I will need to look at the code and experiment. Does your company have a support contact for Serenity? That would allow more time to be spent on the question sooner.

qaEng1213 commented 5 years ago

No, we don't have a support contact. If there is no direct solution you can leave it for when you have more free time :) Thanks again.