如何判断 Android Activity 何时完成加载?
我正在为我们的 Android 应用程序开发自动化测试套件,并在等待活动完全加载时遇到麻烦。我可以调用 getActivity,但仅仅因为它显示了我希望在测试中看到的活动,似乎并不总是意味着该活动的组件已准备好使用(完全加载)。查看 Activity API 没有发现任何结果,其他方法似乎太侵入性并且破坏了测试的初始状态。有谁知道是否有办法询问应用程序或虚拟机当前活动是否已加载?
I'm in the process of working on an automated test suite for our android app, and running into trouble waiting for activities to fully load. I can call getActivity, but just because it shows the activity that I'm hoping to see in my test doesn't always seem to mean that the activity's components are ready for use (fully loaded). Looking through the Activity API didn't turn anything up, and other methods seem too invasive and have spoiled the tests initial state. Does anyone know if there's a way to ask the app or the VM if the current activity is loaded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在扩展
ActivityInstrumentationTestCase2
的测试用例中创建这样的setUp()
方法,您的 Activity 将完全可操作并加载布局,在本例中通过事实上,您可以访问视图及其内容
If you create a
setUp()
method like this in your test case extendingActivityInstrumentationTestCase2<MyActivity>
your Activity will be fully operational and the layout loaded, demonstrated in this case by the fact that you can access the Views and its content
正如我在 评论中提到的,在
onCreate()
早期调用setContentView()
后,您的视图层次结构应该可以正常工作。我从来没有在任何活动或测试类中遇到过这样的问题。我不确定这对于这种特定情况有任何帮助,但一般来说,您可以通过调用
getInstrumentation().waitForIdleSync()
< /a>.这将阻塞,直到没有更多的 UI 事件需要处理。As I mentioned in a comment, your view hierarchy should be working after your call to
setContentView()
early inonCreate()
. I've never had any problems like this with any activity or test class..I'm not sure this is of any help for this specific case, but in general you can determine when the UI event queue is empty by calling
getInstrumentation().waitForIdleSync()
. That'll block until there's no more UI events to process.