How to launch both native app and chrome browser on android to switch between them

Hi All,

Please clarify how to launch appium driver for both native and web view.

I need to test password reset flow for which I have to start from native -> webview -> native

Thanks in advance.

Regards,
Vikram

Hi @VikramVI

you can switch between context and test your password reset flow. Here is sample code.

Set contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
if (contextName.contains(“WEBVIEW”)){
driver.context(contextName);
// code to test password reset field.
}

// once done with password reset test again switch back to Native view

driver.context(“NATIVE”);

Hope this will help.

Regards
Ravi Kumar

@VikramVI
These links can be helpful for better understanding.

how-to-inspect-and-automate-webview-in-hybrid-app

https://discuss.appium.io/t/switch-to-webview-using-getcontexthandles/578/15

https://discuss.appium.io/t/how-to-switch-to-webview-context-using-safariluancher-app-with-c/8600

Hi @ravikr42,

Thanks for quick reply, but I’m still not clear which capabilities to use to create driver object which can be used for both native app and browser operations ?

I tried searching but didn’t get clear answer. Kindly clarify

Thanks in advance.

Regards,
Vikram

Are you testing 2 different apps? or its a Hybrid app which contains both native and WebView? If its a hybrid app u can use single driver object for all operation.

Create a instance of AppiumDriver/AndroidDriver and proceed.

In capabilities part you just need to supply basic device and platform details with app package and app activity.

Regards
Ravi

Hi @ravikr42 Thanks for reply, it’s a native app.
I want to test password reset flow with it.
For this first I need to launch native app, later web view ( chrome browser on android ) and come back to native app.

I am not understanding how to launch chrome browser and native app at same time so that I can switch context between them ?

If you can please share sample code, it will be helpful.

thanks & regards,
Vikram

Hello Vikram,
You can use two instances of AppiumDriver. One for NATIVE application and another for ChromeBrowser.

like,
driver1=new AppiumDriver();
driver2 = new AppiumDriver();

so that you can perform the operations independently on both driver instances :slight_smile:

Hi @sachin925,

Thanks a ton for reply, I was wondering if actions on 2 apps is possible at all with single driver instance.

I’ll try this and update back on this thread.

Thanks & Regards,
Vikram

Hi @VikramVI,

Always welcome :slight_smile:

Just to update on this topic so it’ll be useful to others as well.

I could achieve “Password Reset” flow in below steps

  1. Create Android App driver and initiate password reset. Stop this driver
  2. Create Chrome Browser driver and finish password reset flow. Stop this driver
  3. Create Android App driver and login to app with new password.

This flow takes around 2 m 10 sec on Nexus 5 + Chrome browser

Hi @VikramVI

Did u launch the pre install chrome browser app on your device, I am trying to launch the app on Android device having OS 7.0. Welcome screen of chrome gets displayed all the time whenever I launch the chrome browser app.

Can you please share your experience, how u achieved it

thanks

hello every one i am facing the same kind of problem , i want to test Browser links on native app , when i clicked on links from native app then broswer is opened but from browser i want to check wehter this link is matched with my expected result below is my code can anyone to figure out the problem

@Test(priority = 10)//Validate that FaceBook Link is working Fine
public void ValidateFacebookLink() throws MalformedURLException, InterruptedException {
WebElement FaceBookLinkID = driver
.findElement(By.id(“com.fittingnzidd.cherripik.development:id/tv_share_biz_fb”));
FaceBookLinkID.click();
// CherryPikLaunchInBrowser();
chromeBrowser();

}

public void chromeBrowser() throws InterruptedException {
Thread.sleep(5000);
Set contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
System.out.println(“WebView:”+contextName);
if (contextName.contains(“WEBVIEW”)){
driver.context(contextName);
System.out.println(“WebView:”+contextName);
String ActualURL=driver.getCurrentUrl();
}

These two methods work very well for me checking a context and switching to the one I want. I have noticed for different Things im testing “CHROMIUM” has to change to something else when code is ran it will print the available context’s and just change chromium with the one thats printed out thats not NATIVE_APP

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");

        }

    }