java 如何链接黄瓜中的特征和步骤定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26254604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:34:40  来源:igfitidea点击:

How to link feature and step definition in cucumber

javacucumbercucumber-jvmcucumber-junit

提问by user85

I'm new to Cucumber java and had this problem in initial stages: I'm not using MAVEN project for some reason. I just created a simple java project in eclipse.

我是 Cucumber java 的新手,在初始阶段遇到了这个问题:由于某种原因,我没有使用 MAVEN 项目。我刚刚在 eclipse 中创建了一个简单的 java 项目。

I have my features under "src/dummy/pkg/features", and my implementation "StepDef.java" is under "src/dummy/pkg/features/implementation"

我在“src/dummy/pkg/features”下有我的功能,我的实现“StepDef.java”在“src/dummy/pkg/features/implementation”下

I have written step definitions for Given, When, and Then, but when I run my features file, it is unable to recognize the implementation. How do I link the features with step definitions?

我已经为 Given、When 和 Then 编写了步骤定义,但是当我运行我的功能文件时,它无法识别实现。如何将特征与步骤定义联系起来?

回答by Bala

create a class YourClassand it would look something like the below and run it as JUnit test.

创建一个类YourClass,它看起来像下面这样,并将其作为 JUnit 测试运行。

@RunWith(Cucumber.class)

@CucumberOptions(  monochrome = true,
                     features = "src/dummy/pkg/features/",
                       format = { "pretty","html: cucumber-html-reports",
                                  "json: cucumber-html-reports/cucumber.json" },
                         glue = "your_step_definition_location_package" )

public class YourClass {
  //Run this from Maven or as JUnit
}

回答by vijay

Create a runner class something like this and you should be able to execute. There is also no need to write step definitions manually, just create a feature file and run it, it will create a snippet of the step definition which can be used to create a step definition class:

创建一个像这样的跑步者类,你应该能够执行。也不需要手动编写步骤定义,只需创建一个特征文件并运行它,它将创建一个步骤定义的片段,可用于创建步骤定义类:

A class file called Runnerclass is required to run the cucumber:

运行黄瓜需要一个名为 Runnerclass 的类文件:

@RunWith(Cucumber.class)
@CucumberOptions(plugin={"pretty","html:format"},

features = "Features/name.feature",glue={"path where step definitions exist"})
public class RunnerClass {

}

回答by manojsingh

When you run your Runner class then it will scan all the feature file mentioned within featuresoptions and load the them afterward all step definition within package started by text mentioned within glueoptions will get loaded. For e.g.

当您运行 Runner 类时,它将扫描功能选项中提到的所有功能文件,然后加载它们,然后将加载由胶水选项中提到的文本开始的包中的所有步骤定义。例如

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = { "pretty","json:target/cucumberreports.json" }, 
        glue = "stepDefinition", 
        features = "src/test/resources/TestCases/", 
        tags={"@onlythis"},
        dryRun=false
    )
public class RunTest {

}

Here all the feature file present within

这里存在的所有特征文件

src/test/resources/TestCases/

源代码/测试/资源/测试案例/

will get loaded

将被加载

then all the stepdef within or it's subdirectory will get loaded

然后将加载其中或其子目录中的所有 stepdef

stepDefinition

步骤定义

and whenever your step from feature get run then cucumber will look for function corresponding to step's regex and function will run.

并且每当您从功能运行的步骤时,黄瓜将查找与步骤的正则表达式相对应的功能,并且该功能将运行。

for e.g.

例如

whenever step When User enters email idin src/test/resources/TestCases/Login.feature will run then cucumber will find its corresponding function in all stepdef classes

每当步骤当用户在 src/test/resources/TestCases/Login.feature 中输入电子邮件 ID将运行时,cucumber 将在所有 stepdef 类中找到其对应的函数

Login.feature
@LoginValidation 
Feature: To smoke test functionalities of app 

@Browser @ValidLogin 
Scenario: Verify scenario in case of successful login 
    When User enters email id 
    And User enters password 
    Then User clicks on sign in button and able to sign in 

And moment it will reach class in subdirectory of stepDefinitioni.e. in stepDefinition.ui.home.LoginPageStepDef.javacucumber will find function with @When("^User enters email id$")and will execute this function.

并且当它到达stepDefinition 的子目录中的类时,即在 stepDefinition.ui.home.LoginPageStepDef.java黄瓜将找到带有@When("^User enter email id$") 的函数并执行此函数。

LoginPageStepDef.java
public class LoginPageStepDef {

    LoginPage loginPage = new LoginPage(AttachHooks.driver);
    private static Logger LOGGER = Logger.getLogger(LoginPageStepDef.class);

    @When("^User enters email id$")
    public void user_enters_email_id() throws Throwable {
        //LoginPage.obj = loginPage;
        loginPage.enterEmailId(ConfigManager.getProperty("UserName"));
    }
}