如何从onView结果中获取找到的视图(ViewInteration

时间:2016-04-17 11:03:10

标签: android user-interface testing android-espresso

例如,这是我的代码:

@Test
public void manga_list2() throws Exception {
    onView(withId(R.id.manga_list));
}

我尝试onView(withId(R.id.manga_list))结果是ViewInteration,我希望得到Finded View,我在android studio>中尝试onView(withId(R.id.manga_list)).viewFinder.getView()手表工具,我得到错误:

Executing a query on the view hierarchy outside of the main thread (on: Instr: android.support.test.runner.AndroidJUnitRunner)

如何获取视图?

更新

如果不能,请推荐我可以直接查看的其他ui测试,浓咖啡太黑了

1 个答案:

答案 0 :(得分:0)

"浓缩咖啡太黑了" - 恰恰相反。以下是您可以用来获取活动中任何视图的代码。在示例中,我获得了ListView:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class EspressoTest {

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

    @Test
    public void doSomeStuff() {
        ListView listView = (ListView)mActivityRule.getActivity().findViewById(R.id.manga_list);
    }
}

此处有更多示例 - android-testing-templates