我希望在运行时使用Java获取当前的功能文件名。我有方案信息挂钩但无法获取功能文件
@Before
public void before(final Scenario scenario) {
this.scenario = scenario;
}
我们是否有类似的东西来获取当前的Feature文件名? 我正在使用黄瓜版本1.2.4
答案 0 :(得分:3)
<强>更新强>
这是我对以大写字母开头的功能名称的实现,如示例所示:
private String getFeatureFileNameFromScenarioId(Scenario scenario) {
String featureName = "Feature ";
String rawFeatureName = scenario.getId().split(";")[0].replace("-"," ");
featureName = featureName + rawFeatureName.substring(0, 1).toUpperCase() + rawFeatureName.substring(1);
return featureName;
}
<强> ORIGINAL:强>
我不知道这对你有用,但我建议使用scenario.getId()
这将为您提供功能文件名和方案名称,例如:
Feature: Login to the app
Scenario: Login to the app with password
Given I am on the login screen
When I enter my passcode
Then I press the ok button
使用scenario.getId(),您将获得以下内容:
登录到所述应用内;登录到所述应用内与 - 密码
希望这能帮到你!
答案 1 :(得分:0)
我在Hooks类中使用了以下方法
@Before
public void beforeScenario(Scenario scenario){
// scenarioId = "file:///**/src/test/resources/features/namefeature.feature:99"
String scenarioId=scenario.getId();
int start=scenarioId.indexOf(File.separator+"features"+File.separator);
int end=scenarioId.indexOf(".");
String[] featureName=scenarioId.substring(start,end).split(File.separator+"features"+File.separator);
System.out.println("featureName ="+featureName[1]);
}
答案 2 :(得分:0)
您可以使用Reporter获取当前正在运行的实例,然后从特征文件中提取实际的特征名称,如下所示:
Object[] paramNames = Reporter.getCurrentTestResult().getParameters();
String featureName = paramNames[1].toString().replaceAll("^\"+|\"+$", "");
System.out.println("Feature file name: " + featureName);
答案 3 :(得分:0)
按如下所示创建一个侦听器
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.event.EventHandler;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.TestCaseStarted;
public class Listener implements ConcurrentEventListener {
@Override
public void setEventPublisher(EventPublisher eventPublisher) {
eventPublisher.registerHandlerFor(TestCaseStarted.class, testCaseStartedEventHandler);
}
private final EventHandler<TestCaseStarted> testCaseStartedEventHandler = event -> {
System.out.println("Current file fame : " + event.getTestCase().getUri().toString());
};
}
然后按如下所示向听众提供黄瓜
"-p", "com.myProject.listener.Listener"
这将为您提供功能文件名!
答案 4 :(得分:0)
可能是这样,它仅返回文件名:
private String getFeatureFileNameFromScenarioId(Scenario scenario) {
String[] tab = scenario.getId().split("/");
int rawFeatureNameLength = tab.length;
String featureName = tab[rawFeatureNameLength - 1].split(":")[0];
System.out.println("featureName: " + featureName);
return featureName;
}