在我的情况下,我很难理解和使用依赖注入(inject)。我想使用 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
}
现在我希望有一个包含我的驱动程序、POM 和 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
的类然后我需要把所有东西都连接好,所以所有类(class)可以利用驱动程序、 Hook 和 POM。 Cucumber 不支持继承,所以接口(interface)或依赖注入(inject)(Pico Container)是要走的路。我不知道该怎么做,而且我在网上学习过,我只是无法用我可怜的大脑来解决这一切。
最佳答案
您可能无法实现 继承但是您可以在步骤定义类中使用构造函数将驱动程序对象引用从一个类传递到另一个类。
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 类目的。确保您使用的是 行家 管理所有开发依赖项。以下依赖项应在您的项目中添加 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>
关于java - 依赖注入(inject) - Cucumber Pico 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51125346/