【问题标题】:How to link feature and step definition in cucumber如何在黄瓜中链接特征和步骤定义
【发布时间】:2014-10-08 10:25:13
【问题描述】:

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

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

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

【问题讨论】:

    标签: java cucumber cucumber-jvm cucumber-junit


    【解决方案1】:

    创建一个类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
    }
    

    【讨论】:

    • 没错,我们必须在@CucumberOptions 中使用glue = "pkg location"
    【解决方案2】:

    您必须将您的项目转换为 Cucumber 项目。在 Project Explorer 中右键单击您的项目 > 配置 > 转换为 Cucumber 项目。

    【讨论】:

      【解决方案3】:

      创建一个类似这样的运行器类,您应该能够执行。 也不需要手动编写步骤定义,只需创建一个特征文件并运行它,它会创建一个步骤定义的sn-p,可以用来创建一个步骤定义类:

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

      @RunWith(Cucumber.class)
      @CucumberOptions(plugin={"pretty","html:format"},
      
      features = "Features/name.feature",glue={"path where step definitions exist"})
      public class RunnerClass {
      
      }
      

      【讨论】:

        【解决方案4】:

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

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

        这里所有的特征文件都存在于

        src/test/resources/TestCases/

        将被加载

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

        步骤定义

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

        例如

        每当步骤 当用户在 src/test/resources/TestCases/Login.feature 中输入电子邮件 id 时,黄瓜将在所有 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 
        

        它会到达 stepDefinition 的子目录中的类,即在 stepDefinition.ui.home.LoginPageStepDef.java cucumber 会找到带有 @When("^User enters 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"));
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-06-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多