繁体   English   中英

如何获取当前正在执行的 CucumberOptions 标签?

[英]How to get the currently executing tag of CucumberOptions?

我的功能文件如下:- 功能:以多个角色登录到 HRM

@SMOKE_P1 @REG_P3 场景:SC1_SMOKE_P1:系统管理员应该登录到 HRM 门户给定登录到应用程序:用户“sysadmin”,密码:“sysadmin”

并且 - 单击登录按钮

然后 - 在标题中验证消息:“欢迎配置管理员”

@SMOKE_P2 @REG_P2 场景:SC1_SMOKE_P1:系统管理员应该登录到 HRM 门户给定登录到应用程序:用户“sysadmin”,密码:“sysadmin”

并且 - 单击登录按钮

然后 - 在标题中验证消息:“欢迎配置管理员”

我执行干净测试 -Dcucumber.options="--tags @SMOKE_P1,@SMOKE_P2"

在执行期间,我将场景 ID 检索为:- ScenarioID = 场景.getName().substring(0, 场景.getName().length()); // + // "---:";;

    if (ScenarioID.contains(":")) {
        ScenarioID = ScenarioID.substring(0, ScenarioID.indexOf(":"));
        // ScenarioID.indexOf("\"") + 1,
    }

    StepDefinitionLogger.info("===============================");
    StepDefinitionLogger.info("My Scenario ID:" + ScenarioID);
    StepDefinitionLogger.info("Scenario Tags are: "+scenario.getSourceTagNames().toString());
    StepDefinitionLogger.info("===============================");

因为我的场景有 2 个标签,在执行时只考虑 1 个标签。 我想检索当前正在执行的标签。

可能有点晚了,但如果有人仍然想知道,这是我实现这一目标的方法:

  • 获取作为命令行 -D 参数传递的当前 Cucumberoptions 标签:
private String[] getTagsFromCmdParams() {
    String proptag = System.getProperty("cucumber.options");
    if(proptag != null && proptag.length() > 0) {
        Pattern p = Pattern.compile("--tags (@[^ ]+(,@[^ ]+)*)");
        Matcher m = p.matcher(proptag);
        boolean b = m.matches();
        if(b && m.groupCount() >= 2 ) {
            String test = m.group(1);
            if(test != null && test.length() > 0) {
                String[] bits = test.split(",");
                if(bits.length > 0) {
                    return bits;
                }
            }
        }
    }

    return  new String[]{};
}
  • 从注释中获取当前的 Cucumberoptions 标签(clazz 是@CucumberOptions注释类)
private String[] getTagsFromAnnotations(Class<?> clazz) {
    CucumberOptions co = clazz.getAnnotation(CucumberOptions.class);
    String[] tags = co.tags();

    if(tags.length == 1 && tags[0].contains(",")) {
        return tags[0].split(",");
    }

    return  new String[]{};
}
  • 按此顺序使用两者
public String[] getTags(Class<?> clazz) {
    String[] tags = this.getTagsFromCmdParams();
    if(tags.length > 0) {
        return tags;
    }

    return getTagsFromAnnotations(clazz);
}
  • 然后使用钩子,获取场景标签和选项标签之间的交集
public static String[] getStringIntersection(String[] array1, String[] array2) {
    Set<String> s1 = new HashSet<>(Arrays.asList(array1));
    Set<String> s2 = new HashSet<>(Arrays.asList(array2));
    s1.retainAll(s2);

    return s1.toArray(new String[0]);
}

@Before
public void before(final Scenario scenario) {
    String[] scenarioTags = scenario.getSourceTagNames().toArray(new String[]{});
    String[] optionsTags = getTags(TestRunner.class); // TestRunner has the "@CucumberOptions" annotation
    String[] runningTag = getStringIntersection(scenarioTags, optionsTags);
    System.out.print("scenarioTags: ");
    printArray(scenarioTags);
    System.out.print("optionsTags: ");
    printArray(optionsTags);
    System.out.print("runningTag: ");
    printArray(runningTag); 
}

希望这可以帮助,

干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM