@Test
public void shouldLogUserIn(){
//type in uername/password and close softkeyboard
onView(withId(R.id.username)).perform(typeText("username"));
onView(withId(R.id.userpassword)).perform(typeText("password"), closeSoftKeyboard());
//click login_button
onView(withId(R.id.login_button)).perform(click());
//wait for login background task, should take 5seconds at most
Systemclock.sleep(3000);
--- it fails here even increasing wait time to 2 minutes on slow emulator ---
//rest of the test codes here...
}
以下是引发的错误:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "Andrew Bruce"
View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=640, height=1280, 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, layout-params=WM.LayoutParams{(0,0)(fillxfill) ty=1 fl=#81810100 pfl=0x20000 wanim=0x1030465 needsMenuKey=2}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=640, height=1198, 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, layout-params=android.widget.FrameLayout$LayoutParams@aca3a9f, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-->ViewStub{id=16909288, res-name=action_mode_bar_stub, 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, layout-params=android.widget.LinearLayout$LayoutParams@bf197ec, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+-->FrameLayout{id=-1, visibility=VISIBLE, width=640, height=1157, 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, layout-params=android.widget.LinearLayout$LayoutParams@eafc7b5, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=41.0, child-count=1}
|
+--->ActionBarOverlayLayout{id=2131230776, res-name=decor_content_parent, visibility=VISIBLE, width=640, height=1157, 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, layout-params=android.widget.FrameLayout$LayoutParams@aaf5e4a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+---->ContentFrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=640, height=1062, 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, layout-params=android.support.v7.widget.ActionBarOverlayLayout$LayoutParams@2a1e1bb, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=95.0, child-count=1}
|
+----->LinearLayout{id=-1, visibility=VISIBLE, width=640, height=1062, 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, layout-params=android.widget.FrameLayout$LayoutParams@35646d8, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
我正在使用Espresso框架测试一个简单的Android应用程序。上面的测试在真实的设备上,在快速仿真器上甚至在Circleci上都通过了,但在慢速仿真器上总是失败,我故意这样做。
应该将这种类型的测试视为合格或标记为通过吗?
答案 0 :(得分:0)
您的测试应同时针对快速/慢速设备的互联网连接,不同的屏幕尺寸等通过。您的方法的问题在于,即使不需要这样做,每个设备也将等待3秒。您可以选择等待的最大秒数阈值,但是您应该检查是否在较小的时间间隔内查看视图,以防止进一步等待。想象一下,您将进行80个测试,并且每个测试都需要等待3秒钟才能登录,这将导致4分钟的测试完成时间。在真实的应用中,这种情况会变得更糟。
<div class='header'>
<div class='logo'><img src='...' /></div>
<ul type="none" class='menu'>
<li class='dropdown'><span>Features ▾</span></li>
<ul class='features-menu'>
<li><a href='#'>Harder</a></li>
<li><a href='#'>Better</a></li>
<li><a href='#'>Faster</a></li>
<li><a href='#'>Stronger</a></li>
</ul>
<li><a href='#'>Blog</a></li>
<li><a href='#'>Subscribe</a></li>
<li><a href='#'>About</a></li>
</ul>
</div>
您可以发挥创造力,并使该结构与任何其他断言/匹配器一起使用,我将留给您。完美的等待时间没有确定的配方,您应该自己决定