I have used the accepted solution here and came up with the following code:
Referenced Libraries:
Feature:
Feature: FeatureA
Scenario: ScenarioA
Given
When
Then
Scenario: ScenarioB
Given
When
Then
BaseStep:
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();
}
}
StepA:
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
}
However, the driver "dies" after tearDown()
of ScenarioA and becomes null on Given step of ScenarioB (both scenarios use the same Given). I am not using Maven.
It's because of this line:
private static boolean isInitialized = false;
For each scenario, cucumber creates a new instance for every step file involved. Hence, the driver
in BaseStep
is always null when a scenario starts.
The static isInitialized
boolean is not part of an instance, it's bound to the class it lives in and it's alive until the JVM shuts down. The first scenario sets it to true
, meaning that when the second scenario starts it's still true
and it does not reinitialize the driver in the setUp()
method.
You probably want to make driver
static to share the same instance with both scenarios.
From Luciano's answer, this is what I ended up with:
Features:
Feature: FeatureA
Scenario: ScenarioA
Given I am at the Login page
When
Then
Scenario: ScenarioB
Given I am at the Login page
When
Then
Feature: FeatureB
Scenario: ScenarioC
Given I am at the Login page
When
Then
BaseStep:
public class BaseStep {
protected WebDriver driver = null;
@Before
public void setUp() throws Exception {
driver = SeleniumUtil.getWebDriver(ConfigUtil.readKey("browser"));
}
@After
public void tearDown() {
driver.quit();
}
}
Steps:
public class FeatureAStep {
private WebDriver driver = null;
// PicoContainer injects BaseStep class
public FeatureAStep(BaseStep baseStep) {
this.driver = baseStep.driver;
}
@Given("^I am at the Login page$")
public void givenIAmAtTheLoginPage() throws Exception {
driver.get(ConfigUtil.readKey("base_url"));
}
@When
@When
@Then
@Then
}
public class FeatureBStep {
private WebDriver driver = null;
// PicoContainer injects BaseStep class
public FeatureBStep(BaseStep baseStep) {
this.driver = baseStep.driver;
}
@When
@Then
}
I have 2 Feature files and 2 Step Definition classes. ScenarioC shares the same Given from Scenarios A and B that is defined in FeatureAStep. When and Then of ScenarioC is defined in FeatureBStep. I did not make the WebDriver static in BaseStep using PicoContainer. Both Feature files executed successfully.
Related Reading:
来源:https://stackoverflow.com/questions/45780919/shared-webdriver-becomes-null-on-second-scenario-using-picocontainer