【问题标题】:How to simulate a touch outside of AlertDialog window on android with espresso如何使用 espresso 在 android 上模拟 AlertDialog 窗口之外的触摸
【发布时间】:2015-09-16 05:21:15
【问题描述】:

我的依赖:

androidTestCompile 'com.android.support.test:runner:0.3' 
androidTestCompile 'com.android.support.test:rules:0.3' 
androidTestCompile 'com.android.support:support-annotations:23.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2' 
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2') { 
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude module: 'recyclerview-v7' 
} 
androidTestCompile 'junit:junit:4.12'

我找不到一种方法来模拟在 AlertDialog 窗口之外单击以在它关闭时检查某些内容...

我该怎么做?

【问题讨论】:

  • 你是怎么解决这个问题的?
  • @Rakesh 没有解决:(

标签: android testing integration-testing android-espresso


【解决方案1】:

查看my answer.

Espresso 无法做到这一点。

您需要在 Espresso 测试中使用 uiautomator,将其添加到项目的 gradle 中:

androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'

你的对话框出现后,你可以点击屏幕任意坐标:

UiDevice device = UiDevice.getInstance(getInstrumentation());
        device.click(x,y);

这将关闭您的对话框

【讨论】:

    【解决方案2】:

    在许多情况下,您可以通过创建自定义 ClickAction 来做到这一点:

        public static ViewAction clickXY(final int x, final int y){
        return new GeneralClickAction(
                Tap.SINGLE,
                new CoordinatesProvider() {
                    @Override
                    public float[] calculateCoordinates(View view) {
    
                        final int[] screenPos = new int[2];
                        view.getLocationOnScreen(screenPos);
    
                        final float screenX = screenPos[0] + x;
                        final float screenY = screenPos[1] + y;
                        float[] coordinates = {screenX, screenY};
    
                        return coordinates;
                    }
                },
                Press.FINGER);
    }   
    

    然后您可以在对话框中找到一个已知视图(例如“确定”按钮)并按如下方式调用它:

    onView(withText("OK")).perform(clickXY(-200, 200));
    

    显然,您使用的 x/y 值将取决于您的具体情况。

    【讨论】:

      猜你喜欢
      • 2022-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多