Search code examples
cucumberappiumcucumber-jvmcucumber-junitcucumber-java

Structuring Cucumber JVM step definitions in different java files


I am working on an automation project based on Appium-cucumber-Java, which will be growing over time.

Currently, I have Step definitions Given,When, Then in one file for iOS & another file for Android. Both of these, files extend from a common basetest class. I initialize required Page Objects using new keyword in both of these files.

Now, I would like to modularise it little bit & create a CommonStepDefs file. But I am starting to get nullpointer exception.

Can you please suggest with any method similar to this or sample example to build this

Thanks in advance.

public class AndroidTestsStepDefs_usingFactory extends BaseTestClass {

AndroidChooseCountryPage androidChooseCountryPage;
AndroidCountrySelectionPage androidCountrySelectionPage;
OrderPrints orderPrints;
AndroidHomePage androidHomePage;
TourPage tourPage;

public AndroidTestsStepDefs_usingFactory() throws IOException, AWTException {
}


@Given("^the app has been installed$")
public void the_app_has_been_installed() throws Throwable {
    initializeDriver("android");
    super.setCoreAppType("Android");
}

Solution

  • You are interested in sharing state between your step definitions files.

    The idiomatic to share state in Java is to create a common object that is shared between all steps using dependency injection.

    If your project uses a dependency injection framework, use the same for sharing state between the step definition classes. Cucumber-JVM supports many different dependency injection frameworks. Yours is probably supported.

    If you don't use a dependency injection, I suggest using PicoContainer.

    I have written two blog post on the topic. Sharing state using