Search code examples
angularandroid-espressocapacitor

Access capacitor angular web application with espresso ui test on android - No views in hierarchy found matching Webview


I try to access my capacitor angular web application with espresso ui tests, but always get the following error:

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: an instance of android.webkit.WebView and webView.getSettings().getJavaScriptEnabled() is <true>

The activity and webview is accessable throw capacitor, but how can I let espresso know, which webview to use?

Test source code:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class SimpleOnDeviceUITest {

    @Rule
    public ActivityScenarioRule<MainActivity> activityRule = new ActivityScenarioRule<>(MainActivity.class);

    private <T extends Activity> T getActivity(ActivityScenarioRule<T> activityScenarioRule) {
        AtomicReference<T> activityRef = new AtomicReference<>();
        activityScenarioRule.getScenario().onActivity(activityRef::set);
        return activityRef.get();
    }

    @Test
    public void useAppContext() throws Exception {
        MainActivity activity = getActivity(activityRule);

        WebView webview = activity.getBridge().getWebView();

        onWebView()
            .withElement(findElement(Locator.TAG_NAME, "h1"))
...

Solution

  • The problem ist solved after upgrading to capacitor@4 and adding a sleep before calling onWebView()