lundi 28 mars 2016

Android instrumentation test failing when opening an url and device have multiple apps to open it

I have an Android app with some instrumentations test using Espresso 2.0 where I open an url in a web browser by tapping different UI element. The tests work fine when the device has only one browser app or when a default one is already selected. The problem comes when the device has multiple browser apps and no one is the default one, in that case when the test tries to open the url, a dialog to choose the browser app will be displayed and the test will be stuck there forever. It happens the same if I run the same instrumentation tests on the Cloud Test Lab.

Have anyone faced this problem and have a workaround to make it work?

This an example of one of the tests

 @Test
public void testAttributions() throws Exception {
    List<Attribution> attributions = activityRule.getActivity().mAttributionsProvider.getAttributions();

    onView(withId(R.id.list_attributions)).check(matches(EspressoUtils.withRecyclerViewChildCount(attributions.size())));

    for (int i = 0; i < attributions.size(); i++) {
        Attribution attribution = attributions.get(i);
        onView(withId(R.id.list_attributions))
                .perform(RecyclerViewActions.actionOnItemAtPosition(i, EspressoUtils.checkAttributionView(attribution.getLibrary(),
                        attribution.getAuthor(), attribution.getDescription(), attribution.getLicense())));
    }
}

It's just a recyclerView with cardView items and when you click on each one of them it opens an url using this code

/**
 * Opens an url in a web browser
 *
 * @param context The application's environment
 * @param url     Url to open
 * @throws BrowserNotFoundException If a web browser is not installed on the device
 **/
public static void openUrl(Context context, String url) throws BrowserNotFoundException {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    if (!isIntentAvailable(context, intent)) {
        throw new BrowserNotFoundException();
    }

    context.startActivity(intent);
}

Thanks!

Aucun commentaire:

Enregistrer un commentaire