简体   繁体   English

如何从功能文件中获取场景名称以获取stepdefinition Java代码

[英]How to get the the Scenario name from the feature file to be fetch stepdefinition java code

I wanted to fetch the text name of scenario from the feature file and store it to a variable or print it in my Step Definition. 我想从功能文件中获取方案的文本名称,并将其存储到变量中或将其打印在“步骤定义”中。 Below are my code: 下面是我的代码:

Feature file: 功能文件:

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

Step Definition: 步骤定义:

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>*)
    }
}

I wanted to print the Scenario Outline text: Login with correct credentials. 我想打印“方案大纲”文本:使用正确的凭据登录。 Appreciate your help. 感谢您的帮助。 Thanks! 谢谢!

UPDATE: I've tried adding this code, but throws an error message: 更新:我尝试添加此代码,但引发错误消息: TagExpressionParser异常

You can add a Before Hook and inside the hook print out the scenario name like below: 您可以添加一个“挂钩前”,然后在挂钩内打印出方案名称,如下所示:

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

Below are dependency into Maven pom.xml 下面是对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>

If you not use Maven, download below jars and add them as project library 如果您不使用Maven,请下载以下jar并将其添加为项目库 在此处输入图片说明

Above code worked on my local, you can get them form my github 上面的代码在我的本地上工作,您可以从我的github上获取它们

I've downloaded cucumber jars from io.cucumber where I encountered this error. 我从io.cucumber下载了黄瓜罐,遇到了这个错误。 I created a new project with jars coming from info.cukes then point may build path to it. 我用来自info.cukes的jars创建了一个新项目,然后指向它建立路径。 The scenario.getName() now returns the scenario name as expected. 现在,scheme.getName()返回预期的方案名称。 This solves my issue here. 这在这里解决了我的问题。

You just need to download these jars correctly in maven repository: 您只需要在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

Hope this helps. 希望这可以帮助。

暂无
暂无

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

相关问题 如何在Cucumber中将原始数据json格式从特征文件传递到stepdefinition文件 - How to pass raw data json format from feature file to stepdefinition file in Cucumber 如何使用java从黄瓜的场景大纲中获取场景名称 - How to get scenario name from a scenario outline in cucumber using java 如何将 Cucumber 功能文件中的场景名称作为 Cucumber 步骤中的参数传递? - How to pass Scenario name from the Cucumber feature file as a parameter in the Cucumber steps? 如何在黄瓜java中获取场景名称? - How to get name of scenario in cucumber java? 如何使用Java代码从具有当前日期和时间的文件夹中获取文件名 - How to Fetch a file name from a folder with current Date and Time using Java Code 黄瓜:如何从功能文件运行特定方案 - cucumber: how to run specific scenario from a feature file 如何使用正则表达式从黄瓜功能文件中提取特定场景 - How to pull a specific scenario from a cucumber feature file using regex cucumber:如何从特定功能文件运行标记场景 - cucumber: how to run a tagged scenario from a specific feature file 两个不同功能文件中的多个方案大纲和示例。 如何重用Java步骤从一个功能文件到另一个功能文件? - Multiple Scenario outline and Example in two different features file. How can I reuse the Java steps from one feature file to another? 对于Selenium Java中的某些情况,如何避免在功能文件的背景部分中运行某些步骤 - How to avoid running some steps in Background part in a feature file for some scenario in selenium java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM