是否可以从黄瓜功能文件中传递 Java-Enum 作为参数
Posted
技术标签:
【中文标题】是否可以从黄瓜功能文件中传递 Java-Enum 作为参数【英文标题】:Is it possible to pass Java-Enum as argument from cucumber feature file 【发布时间】:2016-04-15 13:23:51 【问题描述】:我目前在 Java 中使用 selenium,并希望实现 cucumber 以使测试脚本更具可读性。 当前在将参数传递给预期 Enum 作为参数的 java 方法时面临问题。 在迁移当前框架之前,我还想知道 cucumber-java 是否还有其他已知的限制。
由于我是黄瓜的新手,如果有人知道详细学习黄瓜的好来源,请给我一个链接。
【问题讨论】:
您是否尝试过使用转换? @DaveMcNulla 不,我没有使用转换。你能给我举个例子吗? 【参考方案1】:答案是:是的
您可以在场景中使用各种不同的类型:原始类型、自有类 (POJO)、枚举...
场景:
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"
步骤定义代码:
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;
让我知道你的情况。我可以试着帮助你。
【讨论】:
谢谢 实际上,您在这里传递的是字符串格式的参数例如当我提供枚举值“GET”时,我的问题是是否可以使用枚举例如传递参数。当我提供枚举值 'testEnum.GET' 就像我们在 java 中使用的一样。但现在我认为这是不可能的,我必须传递在枚举中定义的确切字符串值。 我认为您尝试做的事情在 Java 技术上是不可能的。您需要告诉函数将接受哪种类型的值。 例如,您可以这样使用:When i select MONDAY in the dropdown
MONDAY 来自 WeekDay 枚举。并且使用 Method @When("^i select (SUNDAY|MONDAY|TUESDAY|WEDNESDAY|THURSDAY|FRIDAY|SATURDAY) in the dropdown") public void iCheckTheCheckboxSelectOpeningHours(CheckoutAddAddressesPage.WeekDay day)....
但你不能使用 'enum.enumValue'
好吧,理论上,您可以使用Transformer
,在点处进行拆分,然后在其上运行Class.forName()
...但是为什么要让自己那么头疼呢?您不仅必须以这种方式指定确切的enum
常量(例如MONDAY
);您还必须精确地指定类名(和包)(例如com.example.WeekDay
)。只要 String 与 enum
常量完全匹配,上面显示的内置工具就会即时将 String 转换为正确的 enum
类型;用于不同的匹配,例如基于enum
值,您可以使用Transformer
,如前所述...
虽然这个答案是正确的,但我用一个更复杂的例子创建了一个新问题,考虑到黄瓜作为与利益相关者沟通的工具的性质,我觉得这是对这个问题的一个很好的补充: ***.com/questions/49898427/…【参考方案2】:
最新的io.cucumber
maven 组不再支持此功能
https://github.com/cucumber/cucumber-jvm/issues/1393
【讨论】:
它再次工作:cucumber.io/blog/2018/10/31/announcing-cucumber-jvm-4-2-0【参考方案3】:这个大约 11 分钟的 youtube 讲座提供了一个很好的方法。 https://www.youtube.com/watch?v=_N_ca6lStrU
例如,
// 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)
...
【讨论】:
【参考方案4】:根据enter link description here
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
【讨论】:
以上是关于是否可以从黄瓜功能文件中传递 Java-Enum 作为参数的主要内容,如果未能解决你的问题,请参考以下文章