[英]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: 更新:我尝试添加此代码,但引发错误消息:
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.