3

在我的情况下,我很难理解和使用依赖注入。我想使用 Pico 容器(https://cucumber.io/blog/2015/07/08/polymorphic-step-definitions)。

这是我的情况......我目前有一个包含我所有硒的一步定义类,而且它变得太大了:

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")

另一个包含 my@Given的类,一个包含 my 的类@When,以及一个包含 my 的类@Then

然后我需要正确连接所有内容,以便所有类都可以使用驱动程序、挂钩和 POM。Cucumber 不支持继承,所以接口或依赖注入(Pico Container)是要走的路。我不知道该怎么做,而且我在网上学习过,我只是无法用我可怜的大脑来解决这一切。

4

2 回答 2

6

您可能对我的博客文章感兴趣,其中我使用 Pico Container 在两个不同的 Cucumber-JVM 步骤类之间共享状态,http://www.thinkcode.se/blog/2017/04/01/sharing-state-between-steps -in-cucumberjvm-using-picocontainer

于 2018-07-03T06:57:13.693 回答
4

您可能无法实现继承,但您可以在步骤定义类中使用构造函数将驱动程序对象引用从一个类传递到另一个类。

  1. 创建一个初始化驱动程序对象/页面对象类的基类/基础类。使用@Before 注释来定义设置,使用@After 来定义拆卸方法。

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();
        }



}

  1. 在您的步骤定义类中,创建一个构造函数并使用上下文实例化 baseFoundation 对象。这就是 pico-container 发挥神奇作用的地方,它将步骤定义类的驱动程序对象与另一个类联系起来。

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 驱动程序对象的包装类中聚合并实例化了我的所有页面对象类。您可以在代码中看到,我正在从对象访问altoroMutualpageObject 类。test

确保您使用maven来管理所有 dev-dependencies 。以下依赖项应在您的项目中添加 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>
于 2018-07-02T05:59:04.177 回答