Search code examples
javadependency-injectioncucumbercucumber-jvmcucumber-serenity

How do I get polymorphic tests working with cucumber-jvm, cucumber-serenity and SerenityObjectFactory


I found an interesting blog explaining in general how to realize polymorphic step definitions using cucumber bdd and picocontainer dependency injection. You can visit the blog here

Where I get stuck is not knowing how to keep the automation logic behind a "generic" interface and then provide two different implementations, One that talks directly to the domain model for unit-integration testing, and another one that uses Selenium WebDriver for UI-testing.

Can someone give me advise/skeleton how to implement this. I can't thank you enough for helping the community.


Solution

  • You can use a Utility class to feed the variables between classes.

    For example, let's take an example of WebDriver driver initiation.

    public class ClassUtility {
    
        public WebDriver baseDriver; 
    
    }
    

    You have a class where you want to access the webDriver.

    public class InventoryPage extends ClassUtility {
    private ClassUtility driver;
    public InventoryPage(ClassUtility driver, ClassUtility fileElementLocator, ClassUtility elementLocatorProperties, ClassUtility page) {
        this.driver= driver;
        this.page =page;
    }
    
    @When("^Open the Google Page$")
    public void openInventoryPage() throws Throwable {  
        driver.baseDriver = new FirefoxDriver();
        driver.baseDriver.get("www.google.com");
    }
    

    If you have to inject to different class, do it the similar fashion.

    public class IntroductoryPage extends ClassUtility {
    
    private ClassUtility driver;
    
    public IntroductoryPage(ClassUtility driver, ClassUtility logger) {
        this.driver= driver;    
    }
    
    @When("^It should go to Account \"([^\"]*)\"")
    public void openIntroductoryPage(String region) throws Throwable {
        driver.baseDriver.findElement(By.linkText("link")")).click();   
    }
    

    Please let me know if you need additional information. You can play around with the dependency injection as you wish.