在我的情况下,我很难理解和使用依赖注入。我想使用Pico容器(https://cucumber.io/blog/2015/07/08/polymorphic-step-definitions).
这就是我的情况.我现在有一个步骤定义类,它包含了我所有的selenium,并且变得太大了:
public class StepDefinitions{
public static WebDriver driver; // a driver is returned here from a static Driver Factory Class
LoginPage loginPage = new LoginPage(driver); //Page Object Model(s)
@Before("setup")
@After //screen snapshot
@After("destroy")
@Given //many methods with this tag
@When //many methods with this tag
@Then //many methods with this tag
}现在,我希望有一个包含我的驱动程序POMs和Hooks的类:
public static WebDriver driver; //driver is returned from a static Driver Factory Class
LoginPage loginPage = new LoginPage(driver); //Page Object Model(s)
@Before("setup")
@After
@After("destroy")另一个包含我的@Given的类,一个包含我的@When的类,一个包含我的@Then的类。
然后,我需要正确地连接所有的东西,这样所有类都可以利用驱动程序、钩子和POMs。黄瓜不支持继承,因此接口或依赖项注入(Pico )是方法之一。我不知道该怎么做,我已经在网上学习过了,我只是不能把我可怜的大脑包围在这一切上。
发布于 2018-07-03 06:57:13
您可能对我的博客文章感兴趣,其中我使用Pico在两个不同的Cucumber-JVM步骤类( http://www.thinkcode.se/blog/2017/04/01/sharing-state-between-steps-in-cucumberjvm-using-picocontainer )之间共享状态。
发布于 2018-07-02 05:59:04
您可能无法实现继承,但是可以使用步骤定义类中的构造函数将驱动程序对象引用从一个类传递到另一个类。
public class Step_Def_Base {
public static webDriverCreator test;
@Before
public void printScenario(Scenario scenario) {
test = new webDriverCreator(this.getClass().getSimpleName());
String className = this.getClass().getCanonicalName();
System.out.println("********************************************************");
System.out.println("Scenario: " + scenario.getName());
System.out.println("********************************************************");
}
@After
public void screenShotAndConsoleLog(Scenario result) {
test.takescreenshot.takeScreenShotOnException(result);
if (!(result.getStatus().contains("pass"))) {
throw new RuntimeException(result.getName() + " got failed");
}
test.closeBrowserSession();
}
}public class StepDefs_AltoroMutualLoginPage {
private Step_Def_Base contextStep;
private webDriverCreator test;
public StepDefs_AltoroMutualLoginPage(Step_Def_Base contextStep) {
// TODO Auto-generated constructor stub
this.contextStep = contextStep; // <-- This is where pico-containers starts working
test = contextStep.test; // <-- Linking your driver object reference from the point where it is instantiated , i.e the base foundation class
}
@Given("^I am on test fire login page \"([^\"]*)\"$")
public void alotoroMutualLoginPage(String url) {
test.launchApplication(url);
test.altoroMutual.launchLoginLink();
test.altoroMutual.verifyUserIsOnLoginPage();
}现在您可以具有创造性,并相应地组织您的页面对象。我在返回web驱动程序对象的包装器类中聚合和实例化了我的所有页面对象类。您可以在代码中看到,我正在从altoroMutual对象访问test pageObject类。
确保您正在使用maven来管理所有开发依赖项。下面的依赖项将在项目中添加pico容器。
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.picocontainer</groupId>
<artifactId>picocontainer</artifactId>
<version>2.14.3</version>
</dependency>https://stackoverflow.com/questions/51125346
复制相似问题