我在这里使用了公认的解决方案并提出了以下代码:
参考图书馆:
特征:
Feature: FeatureA
Scenario: ScenarioA
Given
When
Then
Scenario: ScenarioB
Given
When
Then
基本步骤:
public class BaseStep {
protected WebDriver driver = null;
private static boolean isInitialized = false;
@Before
public void setUp() throws Exception {
if (!isInitialized) {
driver = SeleniumUtil.getWebDriver(ConfigUtil.readKey("browser"));
isInitialized = true;
}
}
@After
public void tearDown() {
driver.quit();
}
}
步骤A:
public class StepA {
private BaseStep baseStep = null;
private WebDriver driver = null;
// PicoContainer injects BaseStep class
public StepA(BaseStep baseStep) {
this.baseStep = baseStep;
}
@Given("^I am at the Login page$")
public void givenIAmAtTheLoginPage() throws Exception {
driver = baseStep.driver;
driver.get(ConfigUtil.readKey("base_url"));
}
@When
@When
@Then
@Then
}
但是,驱动程序在 ScenarioA 之后“死亡”并在tearDown()
ScenarioB 的 Given 步骤中变为 null(两种场景都使用相同的 Given)。我没有使用 Maven。