【问题标题】:Turn on/off location when doing UI Tests Android在进行 UI 测试 Android 时打开/关闭位置
【发布时间】:2020-12-13 22:05:31
【问题描述】:

我正在尝试根据我要测试的内容打开或关闭设备的位置。

我已经查看了Test-Butler,但结果我无法使用它:

Test Butler helper 应用需要一个带有库存 Android 的模拟器图像,否则它将无法安装,因此它不适用于非库存模拟器图像,例如带有 Google API 或 Google Play 的模拟器图像!和

并且我正在我的应用中实现 Google 地图等等 Google API 和 google play 在我的设备上,所以这看起来会导致错误崩溃并且不是我的解决方案案例。

问题:在测试前打开或关闭我应该注意什么?

【问题讨论】:

  • 遗憾的是,这不是我想要的。因为它需要我知道在进入测试时是否启用了位置,然后添加 Espresso 点击以在位置关闭或更糟糕的情况下启用 gps,因为找不到点击并将停止测试位置是否打开。那么是否有一些框架/库可以通过在进入测试之前启用它来帮助我? @HB。
  • So is there some framework/library that could help me by enabling this before entering into a test? 您必须检查位置是否已启用,只有在未启用时才启用。然后就可以运行“测试”了。
  • 啊,明白了。我的错,然后我有点希望更容易,谢谢你的解决方案,如果你想回答这个问题,我会给你正确的答案!

标签: android android-testing android-location ui-testing


【解决方案1】:

您必须检查位置是否已启用,并且仅在未启用时才启用。

您可以像这样检查位置是否启用:

public boolean isLocationEnabled(Context context) { 
    int locationMode = 0; 
    String locationProviders;
    try { 
        locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE); 
    } catch (SettingNotFoundException e) { 
        e.printStackTrace(); 
        return false; 
    } 
    return locationMode != Settings.Secure.LOCATION_MODE_OFF; 
} 

如果上面返回false,那么你可以enable它。然后做你的“测试”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2016-02-25
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多