selenium 是否可以将Java-Enum作为cubble特性文件中的参数传递

lstz6jyr  于 2023-01-20  发布在  Java
关注(0)|答案(4)|浏览(146)

我目前正在使用 selenium 与Java,并希望实现 cucumber ,使测试脚本更具可读性。目前面临的问题时,传递参数到java方法,其中枚举预期作为参数。我还想知道是否有任何其他已知的限制 cucumber java之前,为迁移当前的框架。
因为我是新的 cucumber ,如果任何人知道学习 cucumber 的细节好来源,请给予我一个链接。

qgelzfjb

qgelzfjb1#

答案是:是的
您可以在场景中使用各种不同的类型:基元类型、自有类(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;
        }

    }

}

告诉我你怎么样了。我可以帮你。

ou6hu8tu

ou6hu8tu2#

这篇约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) {
  ...
}
xjreopfe

xjreopfe3#

首先注册一个基于ObjectMapper的转换器,然后就可以按预期使用枚举了。

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
}

Source

相关问题