【问题标题】:Pass Context from JUnit Runner to Cucumber Steps将上下文从 JUnit Runner 传递到 Cucumber Steps
【发布时间】:2016-12-07 17:33:58
【问题描述】:

我将 Cucumber 与 JUnit 跑步者一起使用。我使用 Appium 进行 App UI 测试,但这对于这个问题应该无关紧要。相关部分是我想跨 iOS 和 Android 重用功能和步骤定义,并将 selenium 驱动程序实例从 JUnit 传递到步骤,因为我只想启动一次测试服务器。 p>

我有这样的事情:

login.feature:

Feature: Login
@android_phone @ios_phone
Scenario: Successful login
    Given ...

CucumberIOSTest.java:

@RunWith(Cucumber.class)
@CucumberOptions(
        glue = {"classpath:my.stepdefinitions"},
        tags = {"@ios_phone,@ios_tablet"}
)
public class CucumberIOSTest {

    private static WebDriver driver;

    @BeforeClass
    public static void setUp() throws Exception {
        // setup code that starts up the ios simulator via appium 
        driver = DriverFactory.createIOSDriver();
    }

    @AfterClass
    public static void tearDown() {
        // shutdown ios simulator
    }

}

CucumberAndroidTest.java 中的 Android 几乎相同:

@RunWith(Cucumber.class)
@CucumberOptions(
        glue = {"classpath:my.stepdefinitions"},
        tags = {"@android_phone,@android_tablet"}
)
public class CucumberAndroidTest {

    private static WebDriver driver;

    @BeforeClass
    public static void setUp() throws Exception {
        // setup code that starts up the android simulator via appium 
        driver = DriverFactory.createAndroidDriver();
    }

    @AfterClass
    public static void tearDown() {
        // shutdown android simulator
    }

}

步骤位于GenericSteps.java(目前只有一个文件):

public class GenericSteps {
    public GenericSteps(WebDriver driver) {
        this.driver = driver;
    }

    @Given("^...$")
    public void ...() throws Throwable {
        ...
    }
}

如何将 driverCucumberIOSTest/CucumberAndroidTest 传递给 Steps 构造函数?

问题是只有CucumberIOSTest/CucumberAndroidTest 实例知道我是要测试iOS 还是Android 标签。 GenericSteps 无法知道这一点。另外,我只想为每个平台上的所有测试分别启动一次模拟器。

我尝试了 DI,但没有找到传递我在 JUnit Runner 中创建的 WebDriver 实例的方法。有什么想法吗?

【问题讨论】:

    标签: android ios junit appium cucumber-junit


    【解决方案1】:

    我认为最简单的解决方案是使用Singleton 设计模式。 DriverFactory 必须始终返回相同的 WebDriver 实例。因此,在您的 DriverFactory 中添加“静态 WebDriver 实例”和 getWebDriver() 方法。

    public final class DriverFactory {
      private static WebDriver instance = null;
    
      public static synchronized WebDriver getWebDriver() {
          if (instance == null) {throw RuntimeException("create WebDriver first")};
          return instance;
      }
      public static synchronized WebDriver createAndroidDriver() {
          if (instance == null) { 
             // setup code that starts up the android simulator via appium 
             instance  = yourWebDriver
          }
          return instance;
      }
      ...
    }    
    

    现在您的 GenericSteps cal 看起来像这样:

    public class GenericSteps {
      public GenericSteps() {
          this.driver = DriverFactory.getWebDriver();
      }
    
      @Given("^...$")
      public void ...() throws Throwable {
          ...
      }
    }
    

    【讨论】:

    • 嗯,确实有效,谢谢!我也只需要在@AfterClass 中重置 WebDriver 实例,因此当我第一次运行 iOS 测试然后运行 ​​Android 测试时它可以正常工作。
    猜你喜欢
    • 2018-10-20
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    相关资源
    最近更新 更多