junit.framework.AssertionFailedError:与所选视图不匹配

时间:2017-12-10 10:21:21

标签: android junit android-espresso

我正在使用Espresso测试进行仪器测试。有两个片段替换主Container.I我总是得到视图与所选视图不匹配。我已多次检查但无法找到错误?

RegisterFragmentTest

@RunWith(AndroidJUnit4.class)
public class RegisterFragmentTest {

    private Resources resources;

    @Rule
    public IntentsTestRule<StarterActivity> mActivityIntentsTestRule = new IntentsTestRule<StarterActivity>(StarterActivity.class);

    @Before
    public void init() {
        mActivityIntentsTestRule.getActivity().getSupportFragmentManager().beginTransaction().addToBackStack(null)
                .replace(R.id.pager, new RegisterFragment()).commit();
        resources = mActivityIntentsTestRule.getActivity().getResources();
    }

    @Test
    public void imageHeaderVisibility() throws Exception {
        onView(withId(R.id.imageHeader)).check(matches(isCompletelyDisplayed()));
    }
}

imageview的

   android:id="@+id/imageHeader"

主要容器

  android:id="@+id/pager"

问题

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'at least 100 percent of the view's area is displayed to the user.' doesn't match the selected view.
Expected: at least 100 percent of the view's area is displayed to the user.
Got: "AppCompatImageView{id=2131296361, res-name=imageHeader, visibility=VISIBLE, 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, layout-params=android.widget.RelativeLayout$LayoutParams@52cc5cb4, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:579)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:90)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:314)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:291)
at register.RegisterFragmentTest.imageHeaderVisibility(RegisterFragmentTest.java:41)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: junit.framework.AssertionFailedError: 'at least 100 percent of the view's area is displayed to the user.' doesn't match the selected view.
Expected: at least 100 percent of the view's area is displayed to the user.
Got: "AppCompatImageView{id=2131296361, res-name=imageHeader, visibility=VISIBLE, 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, layout-params=android.widget.RelativeLayout$LayoutParams@52cc5cb4, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}"
  

如何解决这些问题??

1 个答案:

答案 0 :(得分:0)

使用Viewpager

解决了这个问题
@Rule
public ActivityTestRule<StarterActivity> mActivityTestRule = new ActivityTestRule<>(StarterActivity.class);


@Before
public void init() throws Exception {
    onView(allOf(withId(R.id.pager), childAtPosition(childAtPosition(withId(android.R.id.content), 0), 0), isDisplayed()))
            .perform(swipeRight());
}

@Test
public void imageHeaderVisible() throws Exception {
    onView(allOf(withId(R.id.imageHeader), childAtPosition(allOf(childAtPosition(withClassName(is("android.widget.RelativeLayout")), 0)), 0)));
}