如何判断 Android Activity 何时完成加载?

发布于 2024-08-18 20:03:59 字数 198 浏览 8 评论 0原文

我正在为我们的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房的律动 2024-08-25 20:03:59

如果您在扩展 ActivityInstrumentationTestCase2 的测试用例中创建这样的 setUp() 方法,

@Override
protected void setUp() throws Exception {
    super.setUp();

    final MyActivity activity = getActivity();

    tv1 = (EditNumber)activity.findViewById(resId1);
    tv2 = (EditNumber)activity.findViewById(resId2);
}

您的 Activity 将完全可操作并加载布局,在本例中通过事实上,您可以访问视图及其内容

@SmallTest
public void testSimpleCreate() {
    final MyActivity activity = getActivity();
    assertNotNull(activity);

    assertNotNull(tv1);
    assertEquals("mystr1", tv1.getText().toString());
    assertNotNull(tv1);
    assertEquals("mystr2", tv2.getText().toString());
}

If you create a setUp() method like this in your test case extending ActivityInstrumentationTestCase2<MyActivity>

@Override
protected void setUp() throws Exception {
    super.setUp();

    final MyActivity activity = getActivity();

    tv1 = (EditNumber)activity.findViewById(resId1);
    tv2 = (EditNumber)activity.findViewById(resId2);
}

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

@SmallTest
public void testSimpleCreate() {
    final MyActivity activity = getActivity();
    assertNotNull(activity);

    assertNotNull(tv1);
    assertEquals("mystr1", tv1.getText().toString());
    assertNotNull(tv1);
    assertEquals("mystr2", tv2.getText().toString());
}
酒几许 2024-08-25 20:03:59

正如我在 评论中提到的,在 onCreate() 早期调用 setContentView() 后,您的视图层次结构应该可以正常工作。我从来没有在任何活动或测试类中遇到过这样的问题。

我不确定这对于这种特定情况有任何帮助,但一般来说,您可以通过调用 getInstrumentation().waitForIdleSync()< /a>.这将阻塞,直到没有更多的 UI 事件需要处理。

As I mentioned in a comment, your view hierarchy should be working after your call to setContentView() early in onCreate(). 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文