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 limitations of cucumber-java before migrating current framework.
            Asked
            
        
        
            Active
            
        
            Viewed 2.0k times
        
    9
            
            
        - 
                    1Have you tried using transforms? – Dave McNulla Jan 11 '16 at 16:18
- 
                    @DaveMcNulla no I've not used transforms. can you give me example. – Devang Jan 13 '16 at 07:15
4 Answers
8
            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.
- 
                    **thanks** Actually here you are passing argument in String format **e.g. When I supply enum value "GET"** my question was that is it possible to pass argument using enum **e.g. When I supply enum value 'testEnum.GET'** like we use in java. But now i think it is not possible,I've to pass exact String value which is defined in enum. – Devang Jan 13 '16 at 07:12
- 
                    I think what you are trying to do is not possible technically in Java. You need to tell function what type of value will accept. – N.. Jan 13 '16 at 14:50
- 
                    2You can for example us it this way: `When i select MONDAY in the dropdown` Where MONDAY is from WeekDay enum. And with the Method `@When("^i select (SUNDAY|MONDAY|TUESDAY|WEDNESDAY|THURSDAY|FRIDAY|SATURDAY) in the dropdown") public void iCheckTheCheckboxSelectOpeningHours(CheckoutAddAddressesPage.WeekDay day)....` But you cannot use *'enum.enumValue'* – Javatar Mar 01 '16 at 12:35
- 
                    1Well, in theory, you could use a `Transformer`, do a split at the dot, and run a `Class.forName()` on it... But why cause yourself that sort of headache? Not only do you have to specify the exact `enum` constant that way (e.g. `MONDAY`); you'll also have to specify the class name (and packages) precisely (e.g. `com.example.WeekDay`). The built-in facility shown above will turn a String into the right `enum` type on the fly, as long as the String matches the `enum` constant exactly; for matching differently, e.g. based on `enum` values, you can use a `Transformer`, as mentioned previously... – Christian May 03 '17 at 15:47
- 
                    While this answer is correct, I created a new question with a more complex example, which - given the nature of cucumber as a tool to communicate with stakeholders - I feel is a good addition to this question: https://stackoverflow.com/questions/49898427/is-it-possible-to-pass-java-enum-as-argument-from-cucumber-feature-file-in-a-mo – mmalmeida Apr 18 '18 at 11:20
- 
                    Starting from cucumber 4.2.0, you need to use the regexp `(.*)` so that it is recognized by the default transformer, see https://cucumber.io/blog/announcing-cucumber-jvm-4-2-0/ – Julien Kronegg May 28 '19 at 05:31
6
            
            
        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) {
  ...
}
 
    
    
        Brian Hong
        
- 930
- 11
- 15
3
            
            
        First register a transformer based on an ObjectMapper, then you can just use enums as would be expected.
private final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
@DefaultParameterTransformer
@DefaultDataTableEntryTransformer
@DefaultDataTableCellTransformer
public Object defaultTransformer(Object fromValue, Type toValueType) {
    JavaType javaType = objectMapper.constructType(toValueType);
    return objectMapper.convertValue(fromValue, javaType);
}
Scenario: No.6 Parameter scenario enum
    Given the professor level is ASSOCIATE
@Given("the professor level is {}")
public void theProfessorLevelIs(ProfLevels level) {
    System.out.println(level);
    System.out.println("");
}
public enum ProfLevels {
    ASSISTANT, ASSOCIATE, PROFESSOR
}
 
    
    
        AmanicA
        
- 4,659
- 1
- 34
- 49
- 
                    Please include some explanation along with your code. Just a bunch of code is not a good answer. – Victor Jan 17 '23 at 16:30
-1
            
            
        This is no more supported in latest io.cucumber maven group
https://github.com/cucumber/cucumber-jvm/issues/1393
 
    
    
        Hemantkumar Chigadani
        
- 1
- 1
- 1
- 
                    2It works again: https://cucumber.io/blog/2018/10/31/announcing-cucumber-jvm-4-2-0 – M.P. Korstanje Nov 21 '18 at 19:25
 
     
    