【问题标题】:how to get current Cucumber feature file name at runtime using Java如何使用 Java 在运行时获取当前 Cucumber 功能文件名
【发布时间】:2017-05-14 16:05:18
【问题描述】:

我想在运行时使用 Java 获取当前功能文件名。我在钩子中有场景信息,但无法获取功能文件

@Before
    public void before(final Scenario scenario) {
               this.scenario = scenario;
      }

我们有没有类似的东西来获取当前的功能文件名?? 我正在使用黄瓜版本 1.2.4

【问题讨论】:

  • 此功能有一个 PR - github.com/cucumber/cucumber-jvm/pull/984,但不会与发布合并。作为一种解决方法,将功能文件名作为标记添加到具有某种标识符的功能文件中。然后您可以使用 scenario.getSourceTagNames() 来获取所有标签。使用标识符确定带有特征文件名的标签。

标签: cucumber cucumber-jvm cucumber-java


【解决方案1】:

更新:

这是我对以大写字母开头的功能名称的实现,如示例中所示:

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;
}

原文:

我不知道这对你是否有用,但我建议使用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(),您将获得以下信息:

登录到应用程序;使用密码登录到应用程序

希望对您有所帮助!

【讨论】:

  • 当然,这是假设您没有在功能中使用首字母缩略词。
  • 不敢相信 Feature 的原文对 Steps Definition sn-ps 不可用。
【解决方案2】:

我在 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]);
    }

【讨论】:

  • 黄瓜6是不可能的。
【解决方案3】:

您可以使用 Reporter 获取当前正在运行的实例,然后从功能文件中提取我们的实际功能名称,如下所示:

    Object[] paramNames = Reporter.getCurrentTestResult().getParameters();          
    String featureName = paramNames[1].toString().replaceAll("^\"+|\"+$", "");
    System.out.println("Feature file name: " + featureName);

【讨论】:

  • 什么是Reporter?我在 Cucumber API 中找不到这个。
【解决方案4】:

如下创建一个监听器

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"

这将为您提供功能文件名!

【讨论】:

    【解决方案5】:

    可能是这样,它只返回文件名:

    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;
    }
    

    【讨论】:

      【解决方案6】:

      Kotlin 1.5,黄瓜-java 6.10.0:

      @Before
      fun beforeScenario(scenario: Scenario) {
          println(scenario.uri)
      }
      

      在我的例子中打印:

      file:///C:/Users/K.H/git/JvmClient/src/jvmTest/resources/features/C197544.feature
      

      【讨论】:

        猜你喜欢
        • 2018-11-07
        • 2022-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多