Is there a way to test Chrome Custom Tabs with Espresso?
Asked Answered
H

3

12

Here is the stub of the code.

Click data item on ListView . Works as designed and opens Chrome Custom Tab :

onData(anything()).inAdapterView(withId(R.id.listView))
                                       .atPosition(0).perform(click());

Pause(5000);
Espresso.pressBack();

Cannot seem to evaluate anything in the tab or even hit device back button. Getting this error

Error : android.support.test.espresso.NoActivityResumedException: No 
activities  in stage RESUMED.

Did you forget to launch the activity. (test.getActivity() or similar)?

Hairless answered 18/1, 2017 at 21:48 Comment(3)
Please edit your post so that it can be understood without guessing. And consider posting a minimal reproducible example - this helps to understand your problem!Twylatwyman
I also got the same problemFreespoken
Also the same problem here.Lysander
T
3

You can use UIAutomator (https://developer.android.com/training/testing/ui-automator.html). You can actually use both Espresso and UIAutomator at the same time. See the accepted answer on the following post for more information: How to access elements on external website using Espresso

Thury answered 2/8, 2017 at 18:3 Comment(0)
H
2

You can prevent opening Custom Tabs and then just assert whether the intent you are launching is correct:

fun stubWebView(uri: String) {
    Intents.intending(allOf(IntentMatchers.hasAction(Intent.ACTION_VIEW), IntentMatchers.hasData(uri)))
        .respondWith(Instrumentation.ActivityResult(Activity.RESULT_OK, null))
}

fun isNavigatedToWebView(uri: String) {
    Intents.intended(allOf(IntentMatchers.hasAction(Intent.ACTION_VIEW), IntentMatchers.hasData(uri)))
}

This way you can avoid Espresso.pressBack() in your test.

Note that since these are using Espresso Intents, you need to either use IntentsTestRule or wrap these with Intents.init and release like this

fun intents(func: () -> Unit) {
    Intents.init()

    try {
        func()
    } finally {
        Intents.release()
    }
}

intents {
    stubWebView(uri = "https://www.example.com")
    doSomethingSuchAsClickingAButton()
    isNavigatedToWebView(uri = "https://www.example.com")
}
Hendry answered 7/9, 2021 at 17:40 Comment(1)
If you want you can also remove the IntentMatchers.hasData(uri) from the stubWebView() function and prevent opening any WebView (regardless of the URI).Hendry
C
0

A suggestion for improved readability following Mustafa answer:

intents{
    Intents.intended(
        CoreMatchers.allOf(
            IntentMatchers.hasAction(Intent.ACTION_VIEW),
            IntentMatchers.hasPackage("com.android.chrome")
        )

    )
}
Conjunct answered 15/12, 2021 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.