Appium to Select Default Browser Chrome for Android

On some version of phones this is selected as chrome default but on some the test is failing when the user is prompted with a decision Samsung web / chrome for example ;

Any solutions for this ?

@Slf4j
public class MobileSettingsActions extends BaseSettingsActions {

private static final String BROWSER_BUNDLE_ID = "com.android.chrome";

private final AppiumDriver appiumDriver;


@Override
protected void pageShouldBeOpenedInNewBrowserTab(UIElementLocator buttonLocator) {
    try {
      DriverWaiter.await()
                            .timeout(10, TimeUnit.SECONDS)
                            .pollInterval(2, TimeUnit.SECONDS)
                            .until(() -> monosyneDriver.getRequiredElement(buttonLocator).isTouchable());
    } catch (ConditionTimeoutException exception) {
        appiumDriver.isAppInstalled(BROWSER_BUNDLE_ID);
        log.info("App is not installed on device");
    }
    setBrowser();
   Driver.click(buttonLocator);
    browserShouldBeOpened();
    applicationShouldBeOpened();
}

private void setBrowser() {
    DesiredCapabilities.android().setCapability(BrowserType.CHROME, true);
    DesiredCapabilities.chrome().setPlatform(Platform.ANDROID);
 //   appiumDriver.activateApp(BROWSER_BUNDLE_ID);
}

private void browserShouldBeOpened() {
    await().atMost(30, TimeUnit.SECONDS).untilAsserted(() -> assertThat(getCurrentApplicationState() == RUNNING_IN_FOREGROUND)
            .as("Link wasn't opened in browser.")
            .isTrue());
    log.info("Link was opened in browser.");
}

private void applicationShouldBeOpened() {
    Retry.twoTimes(() -> appiumDriver.navigate().back(), ConditionTimeoutException.class);
    await().atMost(30, TimeUnit.SECONDS).untilAsserted(() -> assertThat(getCurrentApplicationState() == RUNNING_IN_BACKGROUND)
            .as("Switch back to application wasn't made.")
            .isTrue());
    log.info("Switch back to application was made.");
}

private ApplicationState getCurrentApplicationState() {
    return appiumDriver.queryAppState(BROWSER_BUNDLE_ID);
}

}

Fragmentation is a real thing. Although you are seeing this now on different devices, you will at some point see some version of this problem on different versions of Android (on the same device, or other devices) as well. Strategies as follows:

  1. Add code to your testing framework that identifies the device/Android version and code a workflow for each such instance under test.
  2. Add code to your testing framework that can identify the anomaly (in this case a new screen) and take the correct action to continue the test. An example of this might be a screen factory that identifies the screen an choses chrome, and then identifies the screen opened by chrome and continues the test.

Strategy 2 is probably a good short term solution. You’ll probably want to code something like solution 1 for long term.