我的功能文件如下: - 功能:登录具有多个角色的HRM
@ SMOKE_P1 @ REG_P3 场景:SC1_SMOKE_P1:系统管理员应该登录HRM门户 登录到应用程序:用户"系统管理员",密码:"系统管理员"
@ SMOKE_P2 @ REG_P2 场景:SC1_SMOKE_P1:系统管理员应该登录HRM门户 登录到应用程序:用户"系统管理员",密码:"系统管理员"
我执行干净测试-Dcucumber.options =" - 标签@ SMOKE_P1,@ SMOKE_P2"
在执行期间,我正在检索方案ID: - ScenarioID = scenario.getName()。substring(0,scenario.getName()。length()); // + //" ---:&#34 ;;;
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个标签。我想检索当前正在执行的Tag。
答案 0 :(得分:0)
可能有点晚了,但是如果有人仍然想知道的话,这就是我实现这一目标的方法:
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
注释的类)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);
}
希望这会有所帮助,
欢呼