title: Switching between two apps in appium iOS
body: <p>I have a testcase where in an iPhone I have to 
  1. Perform a task in app A. 
  2. Switch to Safari browser and perform a task
  3. Switch back again to App A and continue other tasks</p>
  
  <p>Currently I can perform 1 &amp; 2 &amp; then switch back to App A but I cannot perform any tasks in App A after switching from the safari browser. What I did is as follows. </p>
  
  <p>First of all to launch the app A I used the following code</p>
  
  <pre><code>public void createAppiumDriver() throws MalformedURLException, InterruptedException {
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "iPhone 7 Plus");
  capabilities.setCapability("platformName", "iOS");
  capabilities.setCapability("platformVersion", "11.2"); 
  capabilities.setCapability("autoWebView", true);
  capabilities.setCapability("app", getProperties().getProperty("appURL"));
  capabilities.setCapability("autoAcceptAlerts", true);
  capabilities.setCapability("useNewWDA", true);
  capabilities.setCapability("startIWDP",true);
  iosDriver = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);}
  </code></pre>
  
  <p>To switch from my app to Safari I started a new session using the following code to switch to safari</p>
  
  <pre><code>public void openSafari() throws MalformedURLException {
  DesiredCapabilities capabilities2 = new DesiredCapabilities();
  capabilities2.setCapability("deviceName", "iPhone 7 Plus");
  capabilities2.setCapability("platformName", "iOS");
  capabilities2.setCapability("platformVersion", "11.2");
  capabilities2.setCapability("browserName", "Safari");
  capabilities2.setCapability("autoAcceptAlerts", true);
  capabilities2.setCapability("useNewWDA", true);
  iosDriver2 = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities2);
  iosDriver2.get("https://www.gmail.com");}
  </code></pre>
  
  <p>The issue now I have is to switch back to App A again from the safari browser to perform some tasks for that at the moment I have created a new session again as follows</p>
  
  <pre><code>public void switchToNutrifix() throws MalformedURLException, InterruptedException {
  DesiredCapabilities capabilities3 = new DesiredCapabilities();
  capabilities3.setCapability("deviceName", "iPhone 7 Plus");
  capabilities3.setCapability("platformName", "iOS");
  capabilities3.setCapability("platformVersion", "11.2"); //Replace this with your iOS version
  capabilities3.setCapability("autoWebView", true);
  capabilities3.setCapability("app", getProperties().getProperty("appURL"));
  capabilities3.setCapability("autoAcceptAlerts", true);
  capabilities3.setCapability("useNewWDA", true);
  capabilities3.setCapability("startIWDP",true);
  iosDriver3 = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities3);}
  </code></pre>
  
  <p>The issue now is that the app A opens again from Safari but it cannot identify the elements in the app. I even changed the context correctly to webview but still it cannot perform any tasks in App A after switching from Safari browser. Can someone please tell me what I'm doing wrong here or another alternative way to do this switch. </p>
  
  <p>The error I usually get is </p>
  
  <pre><code>May 09, 2018 6:43:42 PM org.openqa.selenium.support.ui.ExpectedConditions findElement
  WARNING: WebDriverException thrown by findElement(By.xpath: //div[@class='login-content purpose']/h2)
  org.openqa.selenium.remote.SessionNotFoundException: A session is either terminated or not started (WARNING: The server did not provide any stacktrace information)
  </code></pre>
  
  <p>and the appium logs display</p>
  
  <pre><code>debug] [iOS] No key id found. Choosing first id from page array
  [debug] [iOS] Page change not referring to currently selected app, ignoring.
  </code></pre>