如何从单元/机器人测试中获取对片段的引用? [英] How do I get a reference to a fragment from within a unit/robotium test?

查看:41
本文介绍了如何从单元/机器人测试中获取对片段的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Robotium 单元测试中获取对片段的引用.在我的情况下,片段包含一个 WebView.我想使用 solo 来获取对片段的引用?如何才能做到这一点?顺便说一句,我之前问过三个机器人/安卓问题,但没有投票,也没有回应!让我们不要忽视测试.请至少重视这个问题.

How do I get a reference to a fragment within Robotium unit test. In my case the fragement houses a WebView. I want to use solo to get reference to the fragment? How can this be done? btw, I have asked three robotium/android questions before without a vote up and without a response! Lets not neglect testing. At least value the question please.

推荐答案

我已经离开 Robotium(和 SO)一段时间了,但您过去可以执行以下操作,因为 Robotium 没有内置的方法来获得片段.

I have been away from robotium (and SO) for a little while but you used to be able to do the following because robotium has no inbuilt way to get the fragment.

第一步是您需要足够简单的当前活动:

The first step is you need the current activity which is easy enough:

Activity current = solo.getCurrentActivity();

如果您查看 Android 活动 API 你会注意到它有 getFragmentManager() 方法,然后如果你遵循它,你会发现名为 findFragmentByXXX();

If you look over the Android activity API you will notice it has the method getFragmentManager() and then if you follow that you will find methods named findFragmentByXXX();

所以代码变成:

片段片段 = solo.getCurrentActivity().getFragmentManager().findFragmentByID(xxx);

Fragment fragment = solo.getCurrentActivity().getFragmentManager().findFragmentByID(xxx);

然后你就会得到你的片段!

you will then have your fragment!

这可能存在一个问题,一些活动没有 getFramentManager() 方法,而是使用方法 getSupportFragmentManager() ,该方法工作相同,但为旧设备提供片段支持,因此您可能需要使用一个或另一个.如果您需要同时支持两者,您将不得不进行一些反射以确定不幸调用哪个方法.

There is one issue with this potentially though, some activities do not have the getFramentManager() method they instead have the method getSupportFragmentManager() which works the same but gave fragment support to older devices, so you may need to use one or the other. If you need to support both you will have to do some reflection to determine which method to call unfortunately.

这篇关于如何从单元/机器人测试中获取对片段的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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