selenium-webdriverdependency-injectioncucumbercucumber-jvmpicocontainer

Does using dependency injection in cucumber-jvm consider bad practise?


I am still very new in Cucumber-jvm/Selenium, so I am very sorry if the question sounds dumb.

Background: I'm an intern in a big company. I'm doing an automated software testing for a product. There is already an existing automated test steps. What we're going to do is to extend the project and add our own steps. The problem is almost all of the steps have kinda same hook methods. I asked a question before on how to avoid to run hook methods and a very good man said to use tags on the hook methods. That's before I found that almost all the hook methods in the previous project are kinda same. This made me think that it's not very fast/optimize because if hook methods are global then every time I run a feature file, it will just do all the hook methods that are same. After days of doing some more coding and research, I found dependency injection using picocontainer and I thought this is a good way of fixing the current issue, but I read some of articles that said dependency injection is consider a bad practice.

My question: Consider what I stated above, does using dependency injection with picocontainer in cucumber-jvm here consider bad practise? If it is, is there more good solution to this?

(Optional Background) I don't think this is important but I'm just going to include it, the hook methods that are almost 95% in every steps:

@Before
public void keepScenario(Scenario scenario){
    this.scenario = scenario;
    fWait = new FluentWait<WebDriver>(BrowserDriver.getCurrentDriver());
    fWait.withTimeout(Duration.ofSeconds(10));
    fWait.ignoring(WebDriverException.class);
}

@After
public void screenshotOnFailure(){
    if (scenario.getStatus().equals("failed")) {
        BrowserDriver.getScreenshot(scenario);
    }
}

Solution

  • Dependency injection solves the problem of sharing state between multiple step definition files in a scenario. Injecting steps into other steps might be considered a bad practice, but on a whole DI itself isn't. But none of this appears to be your direct problem.

    Your problem appears to be that you have multiple hooks that do the same thing. You can either remove these duplicate hooks or use a very strict way to select which features and glue you'll use (check the CucumberOptions on your runner or commandline arguments). If narrowed down to a single class, it will only use the steps and hooks in that class.

    Alternatively you can just remove the duplicate hooks.