Switch App to browser and back to app

My scenario is :

  1. Click “register with Facebook” link in my native app.
  2. Enter gmail id as email and click Login button on facebook login page ( I can perform switch context on emulator).
  3. Click “Login into google” button
  4. https://account.google.com page opened in chrome browser to check the verify code email

I am not able to switch to chrome browser in #4, I don’t know the activity and package of google app here, so I can’t be call switchActivity().

Let me know if you know solution for this scenario.Thanks

@Aru1 when Chrome page will open try 2 things:

  1. driver.getPageSource() - you can see what you see
  2. if first will not help to see Chrome elements try switch context to webView and repeat pageSource.

Hi, is there any way to solve the problem for IOS?

Hi @Mayuresh_Shirodkar, did you get the solution for this. I am also facing the same issue. My scenario is i have to open the chrome , do some validation on chrome page and return back to app.

can Anybody help me . I am automating in both android and ios with laster versions.

Hi I am also facing this issue. Can anyone help me out with this. I click on a link in my app and it opens up the chrome browser. I want to get the url from chrome browser and switch to my native app.

For android just using tap on phone’s back button (using key event) should do: driver.pressKey(new KeyEvent(AndroidKey.BACK));

For iOS; after tapping on deep link it takes to safari browser.
Browser address element: //XCUIElementTypeOther/XCUIElementTypeTextField[@name=“Address”]

Identify this element and get the value attribute to assert or action on it.
How to switch back to native?
Tap on this element so that clear button gets displayed and tap on clear button. Once again type your app’s scheme name in safari address field (every iOS app will have a scheme name; get this from your developer) e.g: driver.getKeyboard().sendKeys(“schemename://\n”);
This would popup the deep link alert, identify this alert - open button and tap on it - this takes you back to the iOS native app (AUT).

1 Like

Hi,It may be too late but it may be useful for other people.
You can do in following way:
//Alreday have driver created with default app which you want to test

//Here is point where you want to switch into another app
JavaScriptExecutor js = (JavaScriptExecutor)driver;
HashMap<String, String> map = new HasjMap<>();
map.put(“bundleId”,"")//ID of the app which you want to switch
js.executeScript(“mobile: lunachApp”, map)
… you can do what you want to do

//Switch to app1 again
driver.activateApp()

1 Like

Here’s how I did it for both iOS and Android (javascript)

returnToApp = async () => {
    if (this.device.platformName == 'Android') {
      await this.driver.pressKeycode(4)
    } else if (this.device.platformName == 'iOS') {
      await this.driver.execute('mobile: activateApp', { bundleId: 'com.my.app' })
    }
    await this.driver.sleep(1000)
  }

I am facing same issue. In my case If I’m tapping from iOS app that navigated safari browser. So how to switchto safari window and back to app ?? kindly suggest any idea…

Try getting context after switching and then once the you verify the link, relaunch the mobile application.

I’m having same issue.
My python appium script click on a button on app1 which open app2.
Then my script does a quick action on app2 and need to come back to app1.

I start_activity again to open app1 but it comes back to homepage of app1. I would lîe it comes back to previous screen.

Any idea?

I have a same problem, i need switch app context to web (safari) and back to app.
when i back to app i can’t use the elements from app.
In Android, is ok but iOS…
i try set a session ID manually but i think is not possible
I using appium version 1.15.1 (1.15.1.20191013.2) and iPhone simulator 13.2 and 11.4.

someone can help me


In the attached screen, as you can see my app which is a hybrid app, after clicking login button, opens device browsers in another withdow and i have to enter the username and password and after “Ok” it should back to the original app.

Am using webdriverIO with appium. Please help @jerimiah797

Can it be done now @jonahss?

I had a same scenario.

  1. Open Native app
  2. Link in app opens up a browser separately and not web view within app.
  3. SO its completely new instance of app thats opened
    And i also didnt want to create new instance of driver to work for browser and native app
    WHat worked for me is i started taking id, class, xpath from browser using UI automator and it worked,
    ex.
    My app with driver uses
    driver.findelementbyid(“com.abc.srt\id:abc”).click();
    now it opens chrome browser and i want to verify url
    driver.findelementbyid(“com.android.chrome\id:url_add”).getText()

This one worked
driver.activateApp(“com.apple.Preferences”);

Aleksei, Please tell me can we do switching apps in ios ?

We can launch other app and execute some actions. E.g. launch safari by bindleID and enter deepLink, confirm it and now back app under test will launching and we check that some screen appears

These are my Switch Context Methods that work very well. I have noticed on certain websites sometimes NATIV_APP is the way to go and other times it’s WEBVIEW so will require some tinkering

Also “CHROMIUM” is not always the correct handle for WEBVIEW so when these methods run, it will print out the AVAILABLE contexts and just change the “CHROMIUM” string to what ever webcontext is printed out

public void switchToWebContext(){
    Set a = CustomDriver.get().getContextHandles();
    System.out.println(a);
    if (CustomDriver.get().getContext().equals("NATIVE_APP")){
        CustomDriver.get().context("CHROMIUM");
        System.out.println("Switched to WebView");
    } else if (CustomDriver.get().getContext().equals("CHROMIUM")){
        System.out.println("Was Already On WebView");
    }
}

public void switchToNativeContext(){
    Set a = CustomDriver.get().getContextHandles();
    System.out.println(a);
    if (CustomDriver.get().getContext().equals("NATIVE_APP")){
        System.out.println("Was Already On Native");
    } else if (CustomDriver.get().getContext().equals("CHROMIUM")){
        CustomDriver.get().context("NATIVE_APP");
        System.out.println("Switched to Native");
    }
}