Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass Java-Enum as argument from cucumber feature file

I am currently using selenium with Java,And want to implement cucumber to make test script more readable. Currently facing issue while passing argument to java method where Enum is expected as parameter. I would also like to know if there are any other known limitaions of cucumber-java befor migrating current framework.

As i'm new on cucumber If any one knowing good source for learing cucumber in details please give me a link.

like image 463
Devang Avatar asked Jan 11 '16 09:01

Devang


2 Answers

The answer is: Yes

You can use all kind of different types in your scenario: primitive types, own classes (POJOs), enums, ...

Scenario :

Feature: Setup Enum and Print value
      In order to manage my Enum
      As a System Admin
      I want to get the Enum

      Scenario: Verify Enum Print
      When I supply enum value "GET"

Step definition code :

import cucumber.api.java.en.When;

public class EnumTest {

    @When("^I supply enum value \"([^\"]*)\"$")
    public void i_supply_enum_value(TestEnum arg1) throws Throwable {
        testMyEnum(arg1);
    }

    public enum TestEnum {
        GET,
        POST,
        PATCH
    }

    protected void testMyEnum(TestEnum testEnumValue) {

        switch (testEnumValue) {
            case GET:
                System.out.println("Enum Value GET");
                break;
            case POST:
                System.out.println("Enum Value POST");
                break;
            default:
                System.out.println("Enum Value PATCH");
                break;
        }

    }

}

Let me know how you are doing. I could try to help you.

like image 87
N.. Avatar answered Sep 28 '22 02:09

N..


This youtube lecture of about 11 minutes gives a good way of doing it. https://www.youtube.com/watch?v=_N_ca6lStrU

For example,

// enum, obviously in a separate file,
public enum MessageBarButtonType {
    Speak, Clear, Delete, Share
}

// method for parameter type. if you want to use a different method name, you could do @ParameterType(name="newMethodName", value="Speak|Clear|Delete|Share") according to the video.
@ParameterType("Speak|Clear|Delete|Share")
public MessageBarButtonType MessageBarButtonType(String buttonType) {
  return MessageBarButtonType.valueOf(buttonType);
}

// use like this. the name inside {} should match the name of method, though I just used the type name.
@Then("Select message bar {MessageBarButtonType} button")
public void select_message_bar_button(MessageBarButtonType buttonType) {
  ...
}
like image 23
Brian Hong Avatar answered Sep 28 '22 03:09

Brian Hong