wtorek, 3 lipca 2018

Cucumber-java and enums

For reusing steps with different values as variables you might simply use enum class. you might of course use switch statement, but where is creativness here?

Feature file

Feature file looks as below:

Feature: As a user i want to visit cucumber page
  Scenario: Visit cucumber as first page and wp as second page
    Given I go to "cucumber" page
      Then I should be on "https://cucumber.io/" page
      When I go to "WP" page
      Then I should be on "https://www.wp.pl/" page

Enum class

Your enum class looks as below:

public enum UrlEnum{

    CUCUMBER("https://cucumber.io/"),
    WP("http://wp.pl"),
    ONET("http://onet.pl");

    public final String urlName;

    UrlEnum(String urlName) {
        this.urlName = urlName;
    }

    public static String getTranslatedEnum(String url) {
        UrlEnum nameToTranslate = UrlEnum.valueOf(url.toUpperCase());
        return nameToTranslate.toString();
    }

    @Override    public String toString() {
        return urlName;
    }
}

Enum class UrlEnum keeps url's strings and with method "getTranslatedEnum(urlName)" you might get value of enum's name- which is passed in "String url" from feature step.

Step in steps class

Step in steps class looks as below:

 @Given("I go to {string} page")
 public void iGoToPage(String urlName){
     urlToGo = UrlEnum.getTranslatedEnum(urlName);
     navigationPageObjects.getPage(urlToGo);
}

In project there might be many enum classes, which keep different categories.

Template

Whole template for "Cucumber-java-enums" you find on github: 
https://github.com/bazylMN/cucumber-java-enum.

Instruction

There is an instruction attached to project, how to work with template, so here I do not write about setting up template. Read README.md file.

Project works as template for cucumber-java framework. It is initial configuration for running tests with Cucumber-java with simple test. All you need is customise it, adding scenarios, steps, page objects and enums.