2016-09-14 12 views
1

약 3 % 시간 실패한 Android UI 테스트를 디버깅하려고합니다.Robotium이 ImageButton 객체를 찾을 수 없어서 때때로 Android UI 테스트가 실패합니다.

우리의 테스트 클래스는 다음과 같이 밖으로 시작 다음과 같이

@RunWith(AndroidJUnit4.class) 
public class ActionWidgetAdapterTest { 

    private Solo solo; 

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

    // SampleContainer is used exclusively for the test case and extends AppCompatActivity 

    @Before 
    public void setUp() throws Exception { 
     solo = new Solo(InstrumentationRegistry.getInstrumentation(), mActivityRule.getActivity()); 
    } 

    @After 
    public void tearDown() throws Exception { 
     solo.finishOpenedActivities(); 
    } 
    // rest of class 
    // [...] 
} 

문제가 테스트 케이스는 다음과 같습니다

@Test 
@LargeTest 
@FlakyTest 
public void testAddActions() throws Exception { 

    final ArrayList<Action> actions = new ArrayList<>(); 

    // Action is our in-house version of the Action class from the Leanback library 
    final Action a1 = new Action(0, "text1", R.drawable.action_button_focused); 
    final Action a2 = new Action(1, "text2", R.drawable.action_button_focused); 
    final Action a3 = new Action(0, "text3", R.drawable.action_button_focused); 
    final Action a4 = new Action(1, "text4", R.drawable.action_button_focused); 

    actions.add(a1); 
    actions.add(a2); 
    actions.add(a3); 
    actions.add(a4); 

    // handler for posting to the main thread 
    Handler mainHandler = new Handler(mActivityRule.getActivity().getBaseContext() 
                   .getMainLooper()); 

    Runnable myRunnable =() -> { 

     // add actions to adapter 
     mActivityRule.getActivity().mActionWidgetAdapter.addActions(actions); 
    }; 
    mainHandler.post(myRunnable); 

    solo.sleep(1000); // pause to resolve any timing issues 
    assertTrue(mActivityRule.getActivity().mActionWidgetAdapter.getItemCount() == 4); 

    // test edge case - navigate all the way to the left 
    solo.sendKey(Solo.LEFT); 
    pressUpDownEnter(); 
    solo.sendKey(Solo.LEFT); 
    pressUpDownEnter(); 
    solo.sendKey(Solo.LEFT); 
    pressUpDownEnter(); 
    solo.sendKey(Solo.LEFT); 
    pressUpDownEnter(); 
    solo.sendKey(Solo.LEFT); 

    assertTrue(solo.getImageButton(0).isFocused()); 
    assertFalse(solo.getImageButton(2).isFocused()); 
} 

테스트 케이스는 대부분의 시간을 전달합니다. 그러나 assertTrue(solo.getImageButton(0).isFocused());이 실행될 때 오류가 발생할 가능성은 거의 없습니다. Robotium은 이러한 상황이 발생하면 "3 ImageButtons을 찾을 수 없다"고 불평합니다. 이것에 어떤 패턴도없는 것 같습니다. Robotium 프레임 워크를 최신 버전으로 업그레이드했지만 문제가 해결되지 않습니다.

누구나 우리가 잘못하고있는 것을 알고 있습니까?

답변

1

나는 그 원인을 발견했다고 생각합니다. 내가 말할 수있는 것부터이 문제는 경쟁 조건 인 tearDown() 때문입니다. Robotium 소스 코드에 따라 finishOpenedActivities()은 뒤로 버튼을 세 번 보내서 작동합니다. 그러나 이것은 별도의 스레드에서 수행되는 것으로 보입니다. 결과적으로 새 테스트 케이스가 시작될 때도 명령이 계속 전송되어 테스트중인 앱이 사라지고 UI를 읽을 수 없게됩니다.

finishOpenedActivities()은 각 테스트가 끝날 때 이미 앱이 종료되었다는 것을 고려하면 다소 불편 해 보입니다. 내가 그 라인을 주석 처리 한 후에 문제가 다시 발생하지 않았다.