【发布时间】: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 {
...
}
}
如何将 driver 从 CucumberIOSTest/CucumberAndroidTest 传递给 Steps 构造函数?
问题是只有CucumberIOSTest/CucumberAndroidTest 实例知道我是要测试iOS 还是Android 标签。 GenericSteps 无法知道这一点。另外,我只想为每个平台上的所有测试分别启动一次模拟器。
我尝试了 DI,但没有找到传递我在 JUnit Runner 中创建的 WebDriver 实例的方法。有什么想法吗?
【问题讨论】:
标签: android ios junit appium cucumber-junit