Reputation: 585
@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...
}
Below is the error thrown:
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}
|
I'm testing a simple Android app using Espresso framework. The above test passes on real device, on a fast emulator and even on Circleci but always fails on a slow emulator, which I intentionally made so.
Should this type of test be considered, marked and concluded as passing or is there a workaround?
Upvotes: 0
Views: 75
Reputation: 158
Your tests should pass for both fast/slow devices internet connection, different screen sizes etc. The problem with your approach is that it will wait 3 seconds for every device even when it does not need to do so. The threshold of max seconds to wait should be your choice but you should check if view is there in small time intervals to prevent further waiting. Imagine that you will have 80 tests and each will need to wait 3 seconds while logging in, which would result in additional 4 minutes for your test to be complete. In a real app, this scenario gets much worse.
for(i in 0..10) {
SystemClock.sleep(300)
try {
onView(withText("Andrew Bruce")).check(matches(isDisplayed())
return
} catch (e : NoMatchingViewException) {
}
You can get creative and make this structure work with any other assertion/matcher, I will leave that to you. There is no certain recipe for the perfect wait time, you should decide that yourself
Upvotes: 1