java - Shared WebDriver becomes null on second scenario using PicoContainer -
i have used accepted solution here , came following code:
referenced libraries:
feature:
feature: featurea scenario: scenarioa given when scenario: scenariob given when
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 @ login page$") public void giveniamattheloginpage() throws exception { driver = basestep.driver; driver.get(configutil.readkey("base_url")); } @when @when @then @then }
however, driver "dies" after teardown()
of scenarioa , becomes null on given step of scenariob (both scenarios use same given). not using maven.
it's because of line:
private static boolean isinitialized = false;
for each scenario, cucumber creates new instance every step file involved. hence, driver
in basestep
null when scenario starts.
the static isinitialized
boolean not part of instance, it's bound class lives in , it's alive until jvm shuts down. first scenario sets true
, meaning when second scenario starts it's still true
, not reinitialize driver in setup()
method.
you want make driver
static share same instance both scenarios.
Comments
Post a Comment