title: How to make Appium clean app before scenario
body: <p>I use Appium along with JUnit. Also Cucumber, but his one shouldn't interfere here.</p>
  
  <p>I need to have clear app installation every case to make them really independent. I use following IOSDriver settings:</p>
  
  <pre><code>    public static void initDriver(boolean noReset) throws MalformedURLException {
          DesiredCapabilities capabilities = new DesiredCapabilities();
          capabilities.setCapability("deviceName", "iPhone 4s");
          capabilities.setCapability("noReset", noReset);
          capabilities.setCapability("autoAcceptAlerts",true);
          driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
      }
  
     public void quitDriver() {
          driver.executeScript("au.backgroundApp(0)");
          driver.quit();
          driver = null;
      }
  </code></pre>
  
  <h2>@Before problem</h2>
  
  <p>What I do is I check if the app is installed before every case, and if it is, to reinstall it. Unfortunately, when I uninstall the app, session is broken and the test case fails. </p>
  
  <p><strong>EDIT:</strong> initDriver() method causes the driver to start and install the app automatically, so this <code>if(isAppInstalled)</code> part is executed even when the app is not installed on device previously.</p>
  
  <pre><code>     @Before
          public void beforeScenario() throws MalformedURLException {
              initDriver(false);
              if(IosConfig.getDriver().isAppInstalled(StringConfiguration.getiOSappBundleId())) {
                  System.out.println("THE APP IS INSTALLED. UNINSTALLING...");
                  IosConfig.getDriver().removeApp(StringConfiguration.getiOSappBundleId());
              }
         }
  </code></pre>
  
  <h2>@After problem [solved]</h2>
  
  <p>Moreover, my @After part is not executed, so the app is not uninstalled after the case... </p>
  
  <pre><code>@After
      public void tearDown(Scenario scenario) {
          try {
              if(scenario.isFailed()) {
                  final byte[] screenshot = IosConfig.getDriver().getScreenshotAs(OutputType.BYTES);
                  scenario.embed(screenshot, "image/png");
                  IosConfig.getDriver().closeApp();
            IosConfig.getDriver().removeApp(StringConfiguration.getiOSappBundleId());
              }
          } finally {
              IosConfig.getDriver().closeApp();
           IosConfig.getDriver().removeApp(StringConfiguration.getiOSappBundleId());
              quitDriver();
  
          }
      }
  </code></pre>
  
  <p>Please help!</p>