Appium iOS: Switching between apps in a test: Is it possible to have more than one instance of WebDriverAgent running at a time

Hello,

Goal
My goal is to automate a test that interacts with an app, which then launches another app to interact with, then finally return to the first app and continue interacting with it.

Progress
For POC purposes, I launched and interacted with MyApp, launched Calendar and interacted with it, then switched back to MyApp to attempt to interact with it.

Issue
I am unable to resume interaction with MyApp for the second time. I am wondering if there is a limitation with WebDriverAgent only allowing one instance at a time. Or could be something else.

Code

public void test(){
           //Storing MyApp which was created earlier
	Driver.driverManager.put("MyApp", Driver.getGeneralDriver());
	
	DesiredCapabilities capabilities = new DesiredCapabilities();
	capabilities.setCapability("platformVersion", "10.2");
	capabilities.setCapability("deviceName", "iPad Air 2");
	capabilities.setCapability("bundleId", "com.apple.mobilecal");
	capabilities.setCapability("platformName", "iOS");
	capabilities.setCapability("noReset", "true");
	capabilities.setCapability("newCommandTimeout", 50);
	
	IOSDriver<IOSElement> driver1 = null;
	try {
                   //Create 2nd driver on same port. This is where a new WebDriverAgent instance is created
		driver1 = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
	} catch (MalformedURLException e) {
		e.printStackTrace();
	}
	driver1.manage().timeouts().implicitlyWait(2, TimeUnit.MILLISECONDS);
	Driver.driverManager.put("Calendar", driver1);
	Driver.driverManager.get("Calendar").findElement(By.name("Week")).click();
           
            //Get point to select MyApp in the multitask view
	Point centerOfWindow = new Point(Driver.driverManager.get("Calendar").manage().window().getSize().getWidth()/6, Driver.driverManager.get("Calendar").manage().window().getSize().getHeight()/2);
	System.err.println("Done clicking calendar");

   //Simulate double clicking the home button to bring up the multitask view
    Runtime runtime = Runtime.getRuntime();
    String[] args = { "osascript", "-e", "tell application \"System Events\" \n tell application \"Simulator\" to activate \n tell application \"System Events\" to keystroke \"hh\" using {command down, shift down} \n end tell" };
    try {
		runtime.exec(args);
	} catch (IOException e) {
		e.printStackTrace();
	}
   
           //Select MyApp. Rough but it works for POC.
	TouchAction touchAction = new TouchAction((MobileDriver) Driver.driverManager.get("Calendar"));
    touchAction
    .press(centerOfWindow.getX(), centerOfWindow.getY())
    .waitAction(500)
    .release()
    .perform(); 

            //Now back to MyApp, but logs will read Session does not exist
	Driver.driverManager.get("MyApp").findElement(By.name("My Accounts")).click();
}

Logs

These are the logs when calling:

Driver.driverManager.get("MyApp").findElement(By.name("My Accounts")).click();



[debug] [MJSONWP] Calling AppiumDriver.findElement() with args: ["name","My Accounts","e87c7b5c-7e53-4eb6-9c99-af7bfb1d4c02"]
[debug] [XCUITest] Executing command 'findElement'
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, name, class name, -ios predicate string, accessibility id
[debug] [BaseDriver] Waiting up to 5000 ms for condition
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/4DF512F5-F1CE-44FC-BE43-3CD65A035BFC/element] with body: {"using":"name","value":"My Accounts"}
[debug] [JSONWP Proxy] Got response with status 200: {"value":"Session does not exist","sessionId":"5F0EA052-5C2C-4640-B2C8-5993335A03FB","status":6}
[debug] [BaseDriver] Waited for 4 ms so far
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/4DF512F5-F1CE-44FC-BE43-3CD65A035BFC/element] with body: {"using":"name","value":"My Accounts"}
[debug] [JSONWP Proxy] Got response with status 200: {"value":"Session does not exist","sessionId":"5F0EA052-5C2C-4640-B2C8-5993335A03FB","status":6}
[debug] [BaseDriver] Waited for 512 ms so far
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/4DF512F5-F1CE-44FC-BE43-3CD65A035BFC/element] with body: {"using":"name","value":"My Accounts"}
[debug] [JSONWP Proxy] Got response with status 200: {"value":"Session does not exist","sessionId":"5F0EA052-5C2C-4640-B2C8-5993335A03FB","status":6}
[debug] [BaseDriver] Waited for 1014 ms so far
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/4DF512F5-F1CE-44FC-BE43-3CD65A035BFC/element] with body: {"using":"name","value":"My Accounts"}
[debug] [JSONWP Proxy] Got response with status 200: {"value":"Session does not exist","sessionId":"5F0EA052-5C2C-4640-B2C8-5993335A03FB","status":6}
[debug] [BaseDriver] Waited for 1517 ms so far
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/4DF512F5-F1CE-44FC-BE43-3CD65A035BFC/element] with body: {"using":"name","value":"My Accounts"}
[debug] [JSONWP Proxy] Got response with status 200: {"value":"Session does not exist","sessionId":"5F0EA052-5C2C-4640-B2C8-5993335A03FB","status":6}
[debug] [BaseDriver] Waited for 2020 ms so far
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/4DF512F5-F1CE-44FC-BE43-3CD65A035BFC/element] with body: {"using":"name","value":"My Accounts"}
[debug] [JSONWP Proxy] Got response with status 200: {"value":"Session does not exist","sessionId":"5F0EA052-5C2C-4640-B2C8-5993335A03FB","status":6}
[debug] [BaseDriver] Waited for 2523 ms so far
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/4DF512F5-F1CE-44FC-BE43-3CD65A035BFC/element] with body: {"using":"name","value":"My Accounts"}
[debug] [JSONWP Proxy] Got response with status 200: {"value":"Session does not exist","sessionId":"5F0EA052-5C2C-4640-B2C8-5993335A03FB","status":6}
[debug] [BaseDriver] Waited for 3029 ms so far
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/4DF512F5-F1CE-44FC-BE43-3CD65A035BFC/element] with body: {"using":"name","value":"My Accounts"}
[debug] [JSONWP Proxy] Got response with status 200: {"value":"Session does not exist","sessionId":"5F0EA052-5C2C-4640-B2C8-5993335A03FB","status":6}
[debug] [BaseDriver] Waited for 3533 ms so far
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/4DF512F5-F1CE-44FC-BE43-3CD65A035BFC/element] with body: {"using":"name","value":"My Accounts"}
[debug] [JSONWP Proxy] Got response with status 200: {"value":"Session does not exist","sessionId":"5F0EA052-5C2C-4640-B2C8-5993335A03FB","status":6}
[debug] [BaseDriver] Waited for 4036 ms so far
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/4DF512F5-F1CE-44FC-BE43-3CD65A035BFC/element] with body: {"using":"name","value":"My Accounts"}
[debug] [JSONWP Proxy] Got response with status 200: {"value":"Session does not exist","sessionId":"5F0EA052-5C2C-4640-B2C8-5993335A03FB","status":6}
[debug] [BaseDriver] Waited for 4543 ms so far
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/4DF512F5-F1CE-44FC-BE43-3CD65A035BFC/element] with body: {"using":"name","value":"My Accounts"}

However, the Session ID differs when interacting with MyApp in the beginning

[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/4DF512F5-F1CE-44FC-BE43-3CD65A035BFC/element] with body: {"using":"name","value":"Options"}

And here’s the Session ID for Calendar

[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/5F0EA052-5C2C-4640-B2C8-5993335A03FB/element] with body: {"using":"name","value":"Week"}

So it looks as if even though I am storing the drivers separately, the session IDs are not stored. Which leads me to think that only one instance of WebDriverAgent can be ran at a time? Or is there another way to do this that I am missing?

I have also tried launchApp(), which indeed launches the app, but MyApp requires a login and my goal is to resume MyApp where I left off instead of the beginning.

Any help, tips, suggestions, or answers will be much appreciated! Thank you!

1 Like

Hi, did you find answers to your question?
May be this will help: