【问题标题】:How to get the the Scenario name from the feature file to be fetch stepdefinition java code如何从功能文件中获取场景名称以获取 stepdefinition java 代码
【发布时间】:2018-02-01 21:57:34
【问题描述】:

我想从功能文件中获取场景的文本名称并将其存储到变量中或将其打印到我的步骤定义中。以下是我的代码:

功能文件:

Feature: Login
  I want to use this template for my feature file

  Scenario Outline: Login with correct credentials
    Given the user is on the login page

步骤定义:

public class Login implements Data{
    WebDriver driver;
    LoginPage loginPage;

    @Given("^the user is on the login page$")
    public void navigateToURL() {
        System.setProperty(DRIVER, DRIVER_LOCATION);
        driver = new FirefoxDriver();

        loginPage = new LoginPage(driver);
        loginPage.visit(BASE_URL);
        loginPage.clickLoginLink();

        //System.out.println("Testing Scenario" + *<scenario name>*)
    }
}

我想打印场景大纲文本:使用正确的凭据登录。感谢你的帮助。谢谢!

更新: 我尝试添加此代码,但会引发错误消息:

【问题讨论】:

    标签: java selenium automation cucumber bdd


    【解决方案1】:

    您可以添加一个Before Hook,并在该钩子内打印出如下所示的场景名称:

    import cucumber.api.Scenario;
    import cucumber.api.java.Before;
    
        @Before
        public void printScenarioName(Scenario scenario) {
            System.out.println("Run into Before Hook: printScenarioName");
            System.out.println("Print Scenario name in Before Hook: " + scenario.getName());
        }
    

    以下是对 Maven pom.xml 的依赖

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- Test tools -->
        <junit.version>4.12</junit.version>
        <!-- Cucumber version -->
        <cucumber.version>1.2.5</cucumber.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java8</artifactId>
            <version>${cucumber.version}</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    

    如果你不使用 Maven,请下载下面的 jars 并将它们添加为项目库

    以上代码在我的本地工作,您可以从my github获取它们

    【讨论】:

    • 不要忘记钩子进入步骤定义文件,而不是带有 RunWith 和 CucumberOptions 注释的 Runner。
    【解决方案2】:

    我从遇到此错误的 io.cucumber 下载了黄瓜罐。我创建了一个新项目,其中包含来自 info.cukes 的 jar,然后指向它可能构建路径。现在,scenario.getName() 按预期返回场景名称。这解决了我的问题。

    您只需要在 maven 存储库中正确下载这些 jar:

    cucumber-core - info.cukes
    cucumber-java - info.cukes
    cucumber-junit - info.cukes
    cucumber-jvm-deps - info.cukes
    cucumber-reporting - net.masterthought
    gherkin - info.cukes
    junit - junit
    mockito-all - org.mockito
    cobertura - net.sourgeforge.cobertura
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多