java 如何从 Espresso 中获取视图以传递到 IdlingResource?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34683956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:11:53  来源:igfitidea点击:

How to get a view from within Espresso to pass into an IdlingResource?

javaandroidandroid-studioandroid-espresso

提问by EGHDK

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

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

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

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

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

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

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

我试过findViewById(),我试过获取当前正在运行的活动,然后调用findViewById()它,但没有运气。

Anyone know what to do in this scenario?

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

回答by EGHDK

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

弄清楚了。要让视图传递到空闲资源中,您所要做的就是获取 ActivityTestRule 的成员变量

For example:

例如:

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

and then just call getActivity().findViewById(R.id.viewId)

然后就打电话 getActivity().findViewById(R.id.viewId)

So the end result is:

所以最终的结果是:

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

回答by Anatolii

The accepted answer works as long as a test is running in the same activity. However, if the test navigates to another activity activityTestRule.getActivity()will return the wrong activity (the first one). To address this, one can create a helper method returning an actual activity:

只要测试在同一活动中运行,接受的答案就有效。但是,如果测试导航到另一个活动activityTestRule.getActivity()将返回错误的活动(第一个)。为了解决这个问题,可以创建一个返回实际活动的辅助方法:

public Activity getCurrentActivity() {
    final Activity[] currentActivity = new Activity[1];
    InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
        @Override
        public void run() {
            Collection<Activity> allActivities = ActivityLifecycleMonitorRegistry.getInstance()
                    .getActivitiesInStage(Stage.RESUMED);
            if (!allActivities.isEmpty()) {
                currentActivity[0] = allActivities.iterator().next();
            }
        }
    });
    return currentActivity[0];
}

And then it could be used as the following:

然后它可以用作以下内容:

Activity currentActivity = getCurrentActivity();
if (currentActivity != null) {
    currentActivity.findViewById(R.id.viewId);
}

回答by Станислав Земляков

If you are using ActivityScenarioRulefrom androidx.test.ext.junit.rules(since ActivityTestRule"will be deprecated and eventually removed from library in the future"), you can get your Activityinstance and call findViewByIdmethod:

如果您正在使用ActivityScenarioRulefrom androidx.test.ext.junit.rules(因为ActivityTestRule“将来会被弃用并最终从库中删除”),您可以获取您的Activity实例并调用findViewById方法:

import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4

@RunWith(AndroidJUnit4::class) {

    @get: Rule
    var testRule = activityScenarioRule<MainActivity>()

    @Test
    fun mainTestCase() {
        testRule.scenario.onActivity { activity ->
            val view = activity.findViewById<YourView>(R.id.view)
        }
    }
}

回答by piotrek1543

I haven't already used IdilingResourcesin Espresso, but did you saw these articles:

我还未使用IdilingResourcesEspresso,但你看到这些文章:

Also please check official Android Docs: Idling Resources (reference)

另请查看官方 Android 文档:空闲资源(参考)

To answer your question,

为了回答你的问题,

Here's an exmple taken from a link above:

这是从上面的链接中获取的示例:

Starting with a context, the root view of the associated activity can be had by

View rootView = ((Activity)_context).Window.DecorView.FindViewById(Android.Resource.Id.Content);

In Raw Android it'd look something like:

View rootView = ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content)

Then simply call the findViewById on this

View v = rootView.findViewById(R.id.your_view_id);

从上下文开始,相关活动的根视图可以通过

View rootView = ((Activity)_context).Window.DecorView.FindViewById(Android.Resource.Id.Content);

在 Raw Android 中,它看起来像:

View rootView = ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content)

然后只需在此调用 findViewById

View v = rootView.findViewById(R.id.your_view_id);

This might be also useful: How to call getResources() from a class which has no context?

这可能也很有用:如何从没有上下文的类调用 getResources()?

Hope it help

希望有帮助