是否可以将 Java-Enum 作为黄瓜功能文件的参数传递
Is it possible to pass Java-Enum as argument from cucumber feature file
我目前正在将 selenium 与 Java 一起使用,并希望实施 Cucumber 以使测试脚本更具可读性。
目前在将参数传递给 java 方法时面临问题,其中 Enum 应作为参数。
我还想知道在迁移当前框架之前是否还有其他已知的 cucumber-java 限制。
因为我是黄瓜的新手如果有人知道学习黄瓜的详细信息,请给我一个link。
答案是:是
您可以在场景中使用各种不同的类型:原始类型、自己的 类 (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;
}
}
}
告诉我你过得怎么样。我可以尽力帮助你。
最新的 io.cucumber
maven 组不再支持此功能
https://github.com/cucumber/cucumber-jvm/issues/1393
这个约 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) {
...
}
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
}
我目前正在将 selenium 与 Java 一起使用,并希望实施 Cucumber 以使测试脚本更具可读性。 目前在将参数传递给 java 方法时面临问题,其中 Enum 应作为参数。 我还想知道在迁移当前框架之前是否还有其他已知的 cucumber-java 限制。
因为我是黄瓜的新手如果有人知道学习黄瓜的详细信息,请给我一个link。
答案是:是
您可以在场景中使用各种不同的类型:原始类型、自己的 类 (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;
}
}
}
告诉我你过得怎么样。我可以尽力帮助你。
最新的 io.cucumber
maven 组不再支持此功能
https://github.com/cucumber/cucumber-jvm/issues/1393
这个约 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) {
...
}
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
}