Gherkin 嵌套步骤的含义
Gherkin Nested Steps meaning
我正在我的项目中编写 Gherkin 测试用例和 Java 步骤定义。我是 Gherkin 的新手,正在尝试理解嵌套步骤的含义。你能帮我理解给出的第二种情况是否涉及嵌套步骤吗?
在我的示例中,我想在给定语句逻辑的第二个场景中重用第一个场景代码。有没有最好的方法来重用或重写逻辑?
注意:下面的例子只是为了解释我的问题。它可能不是一个好的小黄瓜。
Background:
Given The application is opened
Scenario: Successful Login
Given the user name and password are entered
When login button is clicked
Then user login is successful
Scenario: Add Address Successful
Given user login is successful
And Add Address button is clicked
And user city, country are entered
when Submit button is clicked
嵌套步骤是指在 "main" 中调用已定义的步骤。在您的示例中,第一个场景具有登录功能,它将/可以用于所有其他需要用户登录的场景。
因此,第二个场景将有一个 Given
步骤调用第一个场景的登录操作/步骤。有多种方法可以做到这一点:
1. 如果您在同一个 class 中定义这些步骤,只需在不同的步骤/方法中调用相同的方法即可。
像这样:
public class TestStepsOne {
// Steps from first scenario
@Given("^the user name and password are entered$")
public void enterUsernamePassword() throws Throwable {
System.out.println("User and password entered");
}
@When("^login button is clicked$")
public void clickLoginButton() throws Throwable {
System.out.println("Clicked login button");
}
@Then("^user login is successful$")
public void isLoggedIn() throws Throwable {
System.out.println("Logged in!");
}
// All together
@Given("the user is logged in")
public void loginSuccessfully() throws Throwable {
enterUsernamePassword();
clickLoginButton();
isLoggedIn();
}
}
现在您可以在任何场景中使用Given the user is logged in
,它会执行登录操作。
2。使用 Picocontainer -> 详细信息 here
首先你需要将这些依赖添加到你的 pom.xml
:
<dependency>
<groupId>org.picocontainer</groupId>
<artifactId>picocontainer</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
</dependency>
您可以分开您的步骤定义。
像这样:
public class TestStepsOne {
// Same as above, without the nested one
}
第二个class:
public class TestStepsTwo {
private final TestStepsOne testStepsOne;
public TestStepsTwo(TestStepsOne testStepsOne) {
this.testStepsOne = testStepsOne;
}
@Given("the user is logged in")
public void loginSuccessfully() throws Throwable {
testStepsOne.enterUsernamePassword();
testStepsOne.clickLoginButton();
testStepsOne.isLoggedIn();
}
}
3。使用 cuke4duke
-> 详细信息 here ,包括示例
像这样:
public class CallingSteps extends Steps {
public CallingSteps(StepMother stepMother) {
super(stepMother);
}
@When("^I call another step$")
public void iCallAnotherStep() {
Given("the user is logged in"); // This will call a step defined somewhere else.
}
}
希望对您有所帮助
我正在我的项目中编写 Gherkin 测试用例和 Java 步骤定义。我是 Gherkin 的新手,正在尝试理解嵌套步骤的含义。你能帮我理解给出的第二种情况是否涉及嵌套步骤吗?
在我的示例中,我想在给定语句逻辑的第二个场景中重用第一个场景代码。有没有最好的方法来重用或重写逻辑? 注意:下面的例子只是为了解释我的问题。它可能不是一个好的小黄瓜。
Background:
Given The application is opened
Scenario: Successful Login
Given the user name and password are entered
When login button is clicked
Then user login is successful
Scenario: Add Address Successful
Given user login is successful
And Add Address button is clicked
And user city, country are entered
when Submit button is clicked
嵌套步骤是指在 "main" 中调用已定义的步骤。在您的示例中,第一个场景具有登录功能,它将/可以用于所有其他需要用户登录的场景。
因此,第二个场景将有一个 Given
步骤调用第一个场景的登录操作/步骤。有多种方法可以做到这一点:
1. 如果您在同一个 class 中定义这些步骤,只需在不同的步骤/方法中调用相同的方法即可。
像这样:
public class TestStepsOne {
// Steps from first scenario
@Given("^the user name and password are entered$")
public void enterUsernamePassword() throws Throwable {
System.out.println("User and password entered");
}
@When("^login button is clicked$")
public void clickLoginButton() throws Throwable {
System.out.println("Clicked login button");
}
@Then("^user login is successful$")
public void isLoggedIn() throws Throwable {
System.out.println("Logged in!");
}
// All together
@Given("the user is logged in")
public void loginSuccessfully() throws Throwable {
enterUsernamePassword();
clickLoginButton();
isLoggedIn();
}
}
现在您可以在任何场景中使用Given the user is logged in
,它会执行登录操作。
2。使用 Picocontainer -> 详细信息 here
首先你需要将这些依赖添加到你的 pom.xml
:
<dependency>
<groupId>org.picocontainer</groupId>
<artifactId>picocontainer</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
</dependency>
您可以分开您的步骤定义。
像这样:
public class TestStepsOne {
// Same as above, without the nested one
}
第二个class:
public class TestStepsTwo {
private final TestStepsOne testStepsOne;
public TestStepsTwo(TestStepsOne testStepsOne) {
this.testStepsOne = testStepsOne;
}
@Given("the user is logged in")
public void loginSuccessfully() throws Throwable {
testStepsOne.enterUsernamePassword();
testStepsOne.clickLoginButton();
testStepsOne.isLoggedIn();
}
}
3。使用 cuke4duke
-> 详细信息 here ,包括示例
像这样:
public class CallingSteps extends Steps {
public CallingSteps(StepMother stepMother) {
super(stepMother);
}
@When("^I call another step$")
public void iCallAnotherStep() {
Given("the user is logged in"); // This will call a step defined somewhere else.
}
}
希望对您有所帮助