如何从 Espresso 中获取视图以传递到 IdlingResource? [英] How to get a view from within Espresso to pass into an IdlingResource?

查看:22
本文介绍了如何从 Espresso 中获取视图以传递到 IdlingResource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我本质上有一个自定义的 IdlingResource,它接受一个 View 一个构造函数参数.我找不到任何真正谈论如何实施它的地方.

I essentially have a custom IdlingResource that takes a View a constructor argument. I can't find anywhere that really talks about how to implement it.

我正在尝试使用这个答案:https://stackoverflow.com/a/32763454/1193321

I'm trying to use this answer: https://stackoverflow.com/a/32763454/1193321

如您所见,它需要一个 ViewPager,但是当我在我的测试类中注册 IdlingResource 时,我不确定如何获得我的查看.

As you can see, it takes a ViewPager, but when I'm registering the IdlingResource in my test class, I'm not sure how I can get my view.

我已经尝试过 findViewById() 并且我已经尝试获取当前正在运行的活动,然后在其上调用 findViewById(),但没有成功.

I've tried findViewById() and I've tried getting the currently running activity and then calling findViewById() on that, with no luck.

有人知道在这种情况下该怎么做吗?

Anyone know what to do in this scenario?

推荐答案

想通了.要让视图传递到空闲资源中,您所要做的就是获取 ActivityTestRule 的成员变量

Figured it out. To get the view to pass into an idling resource, all you have to do is take the member variable of your ActivityTestRule

例如:

@Rule
public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(
        MainActivity.class);

然后只需调用 getActivity().findViewById(R.id.viewId)

所以最终结果是:

activityTestRule.getActivity().findViewById(R.id.viewId);

这篇关于如何从 Espresso 中获取视图以传递到 IdlingResource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆