由于Robotium无法找到ImageButton对象,Android UI测试偶尔会失败

时间:2016-09-14 05:39:17

标签: java android-testing robotium leanback

我正在尝试调试大约3%时间失败的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抱怨说找不到&#34; 3 ImageButtons&#34;当这个情况发生时。似乎没有任何模式。我将Robotium框架升级到最新版本,但这并没有解决问题。

任何人都知道我们做错了什么?

1 个答案:

答案 0 :(得分:1)

我相信我找到了原因。据我所知,问题是由于tearDown()中的竞争条件造成的。根据Robotium源代码,finishOpenedActivities()通过发送返回按钮三次来工作。但是,这似乎是在一个单独的线程上完成的。因此,即使在新的测试用例开始时,命令仍可能继续发送,导致正在测试的应用程序从视图中消失并且使Robotium无法读取UI。

考虑到应用程序在每次测试结束时已经被杀死,finishOpenedActivities()似乎有点多余。在我评论出这一行之后,这个问题还没有再出现。