【问题标题】:Android testing of the floating view浮动视图的Android测试
【发布时间】:2023-03-21 15:52:02
【问题描述】:

我在 WindowManager 中添加了一个浮动视图,并让它在屏幕上移动,当我点击这个视图时我可以执行点击事件,一切正常。

但是,我不知道如何在 espresso 或 UIAutomator 中访问此视图

将视图添加到 WindowManager

final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    type,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                            | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                            | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                    PixelFormat.TRANSLUCENT
            );

            ImageView floatingView = new ImageView(mContext);
            floatingView.setContentDescription("bugtags_fab_des_normal");
            mWindowManager.addView(floatingView, layoutParams);

浮动视图

矩形中的白蓝图标是我所说的浮动视图。

问题

浮动视图响应点击事件,并执行一些任务,现在我想在 AndroidJunit 测试中执行此操作。

  • 浓缩咖啡

我尝试 Espresso,使用 onView 方法,但测试用例:

onView(withContentDescription("bugtags_fab_des_normal")).perform(click());

获取:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with content description: is "bugtags_fab_des_normal"
  • UIAutomator

我尝试使用 UIAutomator Viewer,但在视图层次结构中找不到 floatingView。

如何

我如何在 espresso 或 uiautomator 中访问此视图并单击它?

附录

测试用例

@Test
public void testInvoke() {
    onView(withContentDescription("bugtags_fab_des_normal")).perform(click());
}

输出日志

output-log-gist

Bugtags.com

实际上,我正在使用一个名为bugtags.com 的sdk,它是一个用于应用程序错误报告和崩溃分析的简单工具。

【问题讨论】:

  • 能否提供完整的 android.support.test.espresso.NoMatchingViewException 日志:
  • 嗨,我已经附加了测试用例和输出日志。
  • 嘿嘿!你是如何实现那个浮动按钮并处理它的点击事件的?请提供它的代码或教程链接!

标签: android android-testing android-espresso android-uiautomator


【解决方案1】:

您的视图在Activity 之外,因此可以使用inRoot() 方法找到它:

@Test
public void checkClickOnFloatingButton() {
    onView(withContentDescription("bugtags_fab_des_normal")).inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView())))).perform(click());
}

另外,您可能应该在这段代码中将reportImage 更改为floatingView

ImageView floatingView = new ImageView(mContext);
reportImage.setContentDescription("bugtags_fab_des_normal"); // <---`reportImage` to `floatingView`
mWindowManager.addView(floatingView, layoutParams);

【讨论】:

  • 太棒了!它就像一个魅力!还有我想问一个问题,为什么要使用withDecorView(not()),这里的not()是什么意思?
  • 可能没有withDecorView(not()) 也能正常工作。我没有尝试。它是另一个项目的副本。但我记得(我可能错了)这种方法我们没有在Activity 中搜索视图,从而减少了测试时间。
  • 好的,我会进一步了解。赏金将在 22 小时后给您。
  • 是否可以使用 UIAutomator 找到这个视图?
【解决方案2】:

在您的 floatingView 上添加一个 onClickListener

【讨论】:

  • 对不起,你错过了我的问题,我想用浓缩咖啡访问这个
猜你喜欢
  • 2019-11-17
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
  • 2015-01-13
相关资源
最近更新 更多