我对Cucumber java还不熟悉,在初始阶段遇到了这个问题: 由于某些原因,我没有使用MAVEN项目。我只是在eclipse中创建了一个简单的java项目。
我的特性文件位于“src/dummy/pkg/features”下,我的实现“StepDef.java”位于“src/dummy/pkg/features/implementation”下
我已经为Given、When和Then编写了步骤定义,但当我运行我的特性文件时,它无法识别实现。我该如何将特性与步骤定义链接起来?
我对Cucumber java还不熟悉,在初始阶段遇到了这个问题: 由于某些原因,我没有使用MAVEN项目。我只是在eclipse中创建了一个简单的java项目。
我的特性文件位于“src/dummy/pkg/features”下,我的实现“StepDef.java”位于“src/dummy/pkg/features/implementation”下
我已经为Given、When和Then编写了步骤定义,但当我运行我的特性文件时,它无法识别实现。我该如何将特性与步骤定义链接起来?
创建一个类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
}
@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/
的功能文件都会被加载,然后所有步骤定义都在其子目录中进行加载。
stepDefinition
每当来自功能文件的步骤被执行时,Cucumber将查找与步骤正则表达式相对应的函数并运行该函数。
例如,每当在src/test/resources/TestCases/Login.feature中运行步骤When User enters email id时,Cucumber会在所有步骤定义类中查找其相应的函数。
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"));
}
}
您需要将项目转换为Cucumber项目。从Project Explorer中右键单击您的项目,然后选择Configure>Convert as Cucumber Project。
@RunWith(Cucumber.class)
@CucumberOptions(plugin={"pretty","html:format"},
features = "Features/name.feature",glue={"path where step definitions exist"})
public class RunnerClass {
}