How do I get a reference to a fragment from within a unit/robotium test?

筅森魡賤 提交于 2019-12-12 21:09:41

问题


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.


回答1:


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();

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();

so the code becomes:

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

you will then have your fragment!

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.



来源:https://stackoverflow.com/questions/18647787/how-do-i-get-a-reference-to-a-fragment-from-within-a-unit-robotium-test

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!