title: Dependence injection to initialize driver in cucumber project and use hook without terminating session
body: <p>I`m trying to implement cucumber with Page Object Model and i faced couple problems and have lots of questions.</p>
  
  <ol>
  <li><p>My iOS app is not that complex, but i still want to orginize all stepdefs and features to correspond with pages from POM. So i will have multiple stepdefs and runners. What is best practice to organize all of it ? I tried Pico DI, but wasn`t even able to pass my driver instance through it.( If you can, please provide structure solution) </p></li>
  <li><p>Since it<code>s native iOS - I</code>m not going to close app after every scenario( it will take forever). But I still want to keep features DRY for re-usability 
  What would be the best way to create one appium driver instance and never create another until feature is executed? I understand i just need to add driver.quit in the last step. What i`m straggling with is to use same driver throughout all project(Pages, Stepdefs) (see code) </p></li>
  <li><p>Im going to run tests with TestNg and wonder if @Before @After annotations still work in stepdefs or it`s better to avoid them ? </p></li>
  <li><p>The MAIN question : Dependency Injection with pico. Since all my tests are acceptance (end to end feature tests) i wonder if it is good idea to create one InjectionSetUp class which will contain all my pages AND driver</p></li>
  </ol>
  
  <p><em>Driver manager</em></p>
  
  <pre class="lang-java prettyprint-override"><code>public class IOSDriverManager {
      public static ThreadLocal&lt;IOSDriver&lt;IOSElement&gt;&gt; webDriver = new ThreadLocal&lt;IOSDriver&lt;IOSElement&gt;&gt;();
  
   public static DesiredCapabilities getIOSCapsLocal() {
          DesiredCapabilities caps = new DesiredCapabilities();
           //My caps
    return caps;
  
   public static void createThreadLocalWebDriver() {
          IOSDriver&lt;IOSElement&gt; driver = null;
  //try catch
        driver = new IOSDriver&lt;IOSElement&gt;(new URL(APPIUM_SERVER_URL), getIOSCapsLocal());
  //try catch
          webDriver.set(driver);
   }
   public static IOSDriver&lt;IOSElement&gt; getThreadLocalDriver() {
          IOSDriver&lt;IOSElement&gt; driver = webDriver.get();
          if (driver == null) {
              createThreadLocalWebDriver();
              driver = webDriver.get();
          }
          return driver;
      }
  </code></pre>
  
  <p><em>BasePage</em></p>
  
  <pre class="lang-java prettyprint-override"><code>public class BasePage {
      IOSDriver&lt;IOSElement&gt; Driver;
      public BasePage(IOSDriver&lt;IOSElement&gt; driver) {
       initElements();
       Driver = driver;
      }
  
      private void initElements() {
  
          PageFactory.initElements(new AppiumFieldDecorator(getDriver()),this);
      }
  
      protected IOSDriver&lt;IOSElement&gt; getDriver() {
          return IOSDriverManager.getThreadLocalDriver();
      }
  
  }
  </code></pre>
  
  <p><em>AnyPage</em></p>
  
  <pre class="lang-java prettyprint-override"><code>public class BiosurveyPage extends BasePage {
   public BiosurveyPage(IOSDriver&lt;IOSElement&gt; driver) {
          super(driver); //  Appiumfield  decorator is applied by BasePage constructor
  //Is it better to just use getDriver() method throughout all pages instead of declaring driver again ? 
      }
  </code></pre>
  
  <p><em>Finaly StepDefs</em></p>
  
  <pre class="lang-java prettyprint-override"><code>public class newSignUpFlowTest{
      protected IOSDriver&lt;IOSElement&gt; getDriver() {
          return IOSDriverManager.getThreadLocalDriver();
      }
      LoginPage poLogin = new LoginPage(getDriver());
      SignUpPage poSignup = new SignUpPage(getDriver());
        // 10+ pages 
  
  @Given("I am on Login Page")
      public void iAmOnLoginPage() {
          assertThat("ON LOGIN PAGE",poLogin.isLoginScreen(),equalTo(true));
      }
  
      @When("I tap on Sign Up Link")
      public void iTapsOnSignUpLink() {
          poLogin.clickSignUp();
      }
  // 20+ methods for this feature
  
  </code></pre>
  
  <p><strong>UPDATE</strong>
  I fixed everything and able to run tests. Now my question is - does my framework look decent ? I don<code>t have any real life experience prior to this. So can someone just approve me and suggest enhancements to keep up with best industry practices ?  I know this post might piss some people off, but i don</code>t know where else to communicate this as i don`t have any friends in the QA field and working remotely </p>