Android Espresso等待查看,R.id值不断变化

发布于 2025-01-11 01:36:47 字数 2466 浏览 0 评论 0原文

我们应用程序的许多部分都有一些视频。在我的测试中,我正在等待视频完成:

onView(isRoot()).perform(waitId(R.id.activity_generic_video_video_control_next, 80000));

waitId() 是我从 StackOverflow 上的一篇文章中获取的一个函数,它在很长一段时间内都运行良好。现在由于某种原因它不起作用,我不确定它何时损坏或为什么损坏。

/** Perform action of waiting for a specific view id. */
    public static ViewAction waitId(final int viewId, final long millis) {
        return new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return isRoot();
            }

            @Override
            public String getDescription() {
                return "wait for a specific view with id <" + viewId + "> during " + millis + " millis.";
            }

            @Override
            public void perform(final UiController uiController, final View view) {
                uiController.loopMainThreadUntilIdle();
                final long startTime = System.currentTimeMillis();
                final long endTime = startTime + millis;
                final Matcher<View> viewMatcher = withId(viewId);

                do {
                    for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                        if (child.getId() != -1) {
                            Log.d(TAG, "child id " + child.getId() + " vs " + viewId + " vs " + R.id.activity_generic_video_video_control);
                        }

                        // found view with required ID and it's visible
                        if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) {
                            return;
                        }
                    }

                    uiController.loopMainThreadForAtLeast(50);
                }
                while (System.currentTimeMillis() < endTime);

                // timeout happens
                throw new PerformException.Builder()
                        .withActionDescription(this.getDescription())
                        .withViewDescription(HumanReadables.describe(view))
                        .withCause(new TimeoutException())
                        .build();
            }
        };
    }

正如您所看到的,代码循环一段时间,直到相关视图可见。有趣的是,我从 for 循环中的日志语句中获取以下输出:

child id 2131363297 vs 2131362267 vs 2131362265

第一个 # 是视图的 ID,给出了为什么要遍历显示树。第二个 # 是传递给函数的 ID。第三个#是同一视图的ID,直接引用R.id。

我可以说至少第二个和第三个数字应该相同,但事实并非如此。这是怎么回事?为什么 id 值会改变?

in many parts of our app we have some videos. and in my tests i am waiting for the video to be finished with this:

onView(isRoot()).perform(waitId(R.id.activity_generic_video_video_control_next, 80000));

waitId() is a function I snagged off of a post here on StackOverflow, and it worked very well for a long time. Now for some reason it's not working and I am not sure when this broke or why.

/** Perform action of waiting for a specific view id. */
    public static ViewAction waitId(final int viewId, final long millis) {
        return new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return isRoot();
            }

            @Override
            public String getDescription() {
                return "wait for a specific view with id <" + viewId + "> during " + millis + " millis.";
            }

            @Override
            public void perform(final UiController uiController, final View view) {
                uiController.loopMainThreadUntilIdle();
                final long startTime = System.currentTimeMillis();
                final long endTime = startTime + millis;
                final Matcher<View> viewMatcher = withId(viewId);

                do {
                    for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                        if (child.getId() != -1) {
                            Log.d(TAG, "child id " + child.getId() + " vs " + viewId + " vs " + R.id.activity_generic_video_video_control);
                        }

                        // found view with required ID and it's visible
                        if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) {
                            return;
                        }
                    }

                    uiController.loopMainThreadForAtLeast(50);
                }
                while (System.currentTimeMillis() < endTime);

                // timeout happens
                throw new PerformException.Builder()
                        .withActionDescription(this.getDescription())
                        .withViewDescription(HumanReadables.describe(view))
                        .withCause(new TimeoutException())
                        .build();
            }
        };
    }

As you can see, the code loops for a period of time until the view in question is visible. The interesting part is I am getting this output from my log statement in the for loop:

child id 2131363297 vs 2131362267 vs 2131362265

The first # is the ID of the view given why traversing the display tree. The second # is the ID that is passed into the function. The third # is the ID of the same view while referencing the R.id directly.

I can say that at least the 2nd and 3rd numbers should be the same, but they aren't. What's going on? Why is the id value changing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦明 2025-01-18 01:36:47

activity_generic_video_video_control_next 资源是什么?如果是动画,它可以更改 res id。

您应该收到一个错误解释:

也许主线程在合理的时间内没有进入空闲状态?可能有动画或某些东西不断地重新绘制屏幕。或者该活动正在创建时进行网络调用?查看线程转储日志。

由于您正在播放视频,如果重新绘制画布(连同按钮),则由以下原因引起:

...动画或不断重新绘制屏幕的东西。

What is the resource activity_generic_video_video_control_next? If it's an animation it can change res id.

You should have gotten an error explaining this:

Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs.

Since you're playing a video, if that canvas is redrawn (along with the button) it's caused by:

...an animation or something constantly repainting the screen.

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