Can anybody share the code to switch to chrome browser and then switch back to the app we were automating

Please share the code to switch from an app to chrome browser and then switch back to the app I am automating.
I tried but failed!

first pls clear where is “chrome” to open? same phone or PC where tests are running?

Hi @Aleksei

I need to perform the complete operation on a single device.
Like I am automating an application A from here I need to open some specific URL in chrome in the same device and now I again need to switch back to application A.

@jitenderbhardwaj when you need to start chrome try something like:


import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidKeyCode;

        ((AndroidDriver) driver).startActivity(new Activity("chrome_app_package", "chrome_app_activity"));
        ((AndroidDriver) driver).get("your_url");
        ((AndroidDriver) driver).pressKeyCode(AndroidKeyCode.BACK); //close browser with back key

@Aleksei
I’m missing something. Can you help me out?

@jitenderbhardwaj your code is wrong (not startActivity but driver.startActivity…). i updated mine code with imports. just copy paste.

note: mine java-client version is:

            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>5.0.0-BETA8</version>

@Aleksei
The issue isn’t with the code.
I guess the issue is with the chrome activity declaration or chrome itself.
Because same code is working for other apps like Gmail, Mozilla Firefox etc.

Have you tried:
pkg = “com.android.chrome”;
activity = “com.google.android.apps.chrome.Main”;
?

@vrunoaThanks.
You are right I was giving some other activity name that is why I was getting: permission denied.
And when I used the activity name which you mentioned, it resolved my issue.

1 Like