Quizp2p / android-test-kit

Automatically exported from code.google.com/p/android-test-kit
0 stars 0 forks source link

Espresso won't find View when included with androidTestCompile #170

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This is darn strange. I have an Activity with a ViewPager that hosts a couple 
of of Fragments, the first one has a RadioButton with the 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(withText(R.string.button_userrole_backjudge)).perform(click());

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

    }

}
When Espresso is imported in my 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 fine.

When I use the intended variant of importing dependencies for instrumentation 
tests:

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 of compile the test fails because the View with 
the provided id is 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(De
faultFailureHandler.java:82)
at 
android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHa
ndler.java:53)
at 
android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInt
eraction.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)
It seems like Espresso only checks the Views in the Activity's layout, not the 
ones provided by the ViewPager.

a. How comes my test works when using compile instead of androidTestCompile?

b. Is Espresso even supposed to find Views within Fragments inside a ViewPager?

Original issue reported on code.google.com by fmwe...@gmail.com on 7 Aug 2015 at 6:42