嵌套步骤指的是调用“主”中的定义步骤。在您的示例中,第一种方案具有登录功能,可以在所有其他需要用户登录的情况下使用。
因此,第二种方案将有一个调用登录操作的Given
步骤/第一种情景的步骤。有多种方法可以做到这一点:
1.如果您在同一个类中定义这些步骤,只需在不同的步骤/方法中调用相同的方法即可。
像这样:
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
}
和第二类:
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.
}
}
希望这会有帮助
感谢您的解释。我想尝试使用pico容器。为了使用它,我应该只添加依赖项并且有一个参数化的构造函数吗?它是如何工作的?此外,我的CommonSteps调用(或您的示例中的TestStepsOne类)具有参数化的构造函数。这会被处理吗? – kav12345
请查看picocontainer链接的网站,它比我在评论中更好地解释了它(还提供了实施示例)。 –