Dependency Injection - Cucumber Pico Container

snikt picture snikt · Jul 1, 2018 · Viewed 16.1k times · Source

I am having difficulty understanding and utilizing dependency injection in my situation. I want to use Pico-container (https://cucumber.io/blog/2015/07/08/polymorphic-step-definitions).

This is my situation...I currently have one step definition class that contains all my selenium, and which is getting way too big:

public class StepDefinitions{
    public static WebDriver driver; // a driver is returned here from a static Driver Factory Class
    LoginPage loginPage = new LoginPage(driver); //Page Object Model(s)

    @Before("setup")
    @After //screen snapshot
    @After("destroy")
    @Given //many methods with this tag
    @When  //many methods with this tag
    @Then  //many methods with this tag
}

Now I want to potentially have one class that contains my driver, POMs, and Hooks:

public static WebDriver driver; //driver is returned from a static Driver Factory Class
LoginPage loginPage = new LoginPage(driver); //Page Object Model(s)

 @Before("setup")
 @After
 @After("destroy")

Another class that contains my @Given, one class that contains my @When, and one class that contains my @Then

I then need to connect everything up right, so all classes can utilize the driver, hooks, and POMs. Cucumber does not support inheritance, so interfaces or Dependency Injection (Pico Container) is the way to go. I do not know how to do this, and I have studied online, I just cannot wrap my poor brain around it all.

Answer

Thomas Sundberg picture Thomas Sundberg · Jul 3, 2018

You might be interested in my blog post where I use Pico Container to share state between two different Cucumber-JVM step classes, http://www.thinkcode.se/blog/2017/04/01/sharing-state-between-steps-in-cucumberjvm-using-picocontainer