Espresso will not search for View if included in androidTestCompile

This is weird. I have a Activity

s ViewPager

that contains a pair of Fragment

s, the first one has RadioButton

an id android:id="@+id/backjudgeRadionButton"

.

I have an Espresso test that looks like this:

import android.test.ActivityInstrumentationTestCase2;

import model.GameSetup;
import ui.SetupActivity;
import weigl.fm.refwatch.R;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

/**
 * Created by asco on 8/7/15.
 */
public class SetupActivityEspressoTest extends ActivityInstrumentationTestCase2<SetupActivity> {


    public SetupActivityEspressoTest() {
        super(SetupActivity.class);
    }


    @Override
    protected void setUp() throws Exception {
        super.setUp();
        getActivity();
    }

    public void testUserRoleIsSet() {


        onView(withId(R.id.backjudgeRadionButton)).perform(click());

        assertEquals(GameSetup.UserRole.backjudge, getActivity().getGameSetup().getUserRole());

    }

}

      

When Espresso is imported into mine build.gradle

via

compile('com.android.support.test.espresso:espresso-core:2.2') {
    exclude module: 'support-annotations'
}

compile('com.android.support.test:runner:0.3') {
    exclude module: 'support-annotations'
}
compile('com.android.support.test.espresso:espresso-contrib:2.2') {
    exclude module: 'support-annotations'
}

      

The test works great.

When I use the intended dependency import option for my benchmarks:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
    exclude module: 'support-annotations'
}

androidTestCompile('com.android.support.test:runner:0.3') {
    exclude module: 'support-annotations'
}
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2') {
    exclude module: 'support-annotations'
}

      

with androidTestCompile

instead, the compile

test failed because the View with the provided ID was not found:

Running tests
Test running started
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131230756>

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=280, height=280, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=280, height=280, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-->ViewStub{id=16909171, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+-->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=280, height=280, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+--->LinearLayout{id=-1, visibility=VISIBLE, width=280, height=280, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+---->ViewPager{id=2131558442, res-name=viewPager, visibility=VISIBLE, width=280, height=280, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=true, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0}
|
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:82)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at SetupActivityEspressoTest.testUserRoleIsSet(SetupActivityEspressoTest.java:30)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)

      

Espresso seems to only check the views in the Activity layout, not the ones provided by the ViewPager.

and. How does my test work when used compile

instead androidTestCompile

?

b. Is Espresso really supposed to find View

inside Fragments

inside a ViewPager

?

EDIT: This is the second espresso test case I tried, taken from the new Android test pattern :

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


    @Rule
    public ActivityTestRule<SetupActivity> mActivityRule =
            new ActivityTestRule<>(SetupActivity.class);

    @Test
    public void findViewPerformActionAndCheckAssertion() {
        // Find Button and Click on it
        onView(withId(R.id.backjudgeRadionButton)).perform(click());

    }

}

      

It shows the same effect.

This all happens inside the module wear

, if that matters.

EDIT2: You can look at the whole project on GitHub .

+3


source to share


1 answer


The only reason I could think of is this: One of the espresso library dependencies (my bids are in one of the support libraries) also depends on the UI library being loaded (com.google.android.support:wearable). The Espresso version of this dependency library is newer than the wearable version. If you enable Espresso as a "compilation", a newer version of this library is used and you should be fine. When you use it as a dependency "androidTestCompile", the older version is used to build your application.



I would suggest that you see if there is a later version of the downloadable UI library (which should have the latest dependencies) or figure out what the dependency is and get the most recent version for yourself (and exclude that from Espresso and the wearable UI lib).

+3


source







All Articles