0

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.

makhlo
  • 346
  • 1
  • 5
  • 16

1 Answers1

1

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.

Mathan
  • 162
  • 1
  • 2
  • 14
  • First of all, thanks for the quick response and your comprehensive explanation. What I need is to have two different implementations from the same generic interface. One implementation for Integration testing and an other implementation for GUI test. How this generic interface with method (s) looks like is for me a mystery. It depends on the type of test, which dependencies are needed. For GUI testing, inject the web driver, but not for integration tests. Maybe inject something else or no dependencies at all. Thanks in advance and new idea's are welcome;) – makhlo Jun 14 '17 at 16:10
  • Can you tell your requirement? Specification like what you need to do in GUI testing and Integration testing. The dependency injection can be extended over to any object over any class. It depends on what you define in Class utility and extend it in your implementation class. What kind of dependencies you need to inject for your GUI vs Integration testing? – Mathan Jun 15 '17 at 08:29
  • For GUI the dependency is webdriver, but for integration testing it depends, it could be testdoubles, mockings. An example: – makhlo Jun 16 '17 at 13:46
  • 1
    `public class SomeStepDefs { public SomeStepDefs(AutomationApi api) { } }` `public class SomeOtherStepDefs { public SomeOtherStepDefs(AutomationApi api) { } }` `addClass(WebAutomationApi.class);` OR `addClass(IntegrationAutomationApi.class);` WebAutomationApi IS an AutomationApi in Step definition constructor IntegrationAutomationApi IS an AutomationApi in Step definition constructor. – makhlo Jun 16 '17 at 13:58