android - 使用 Espresso on Emulator 在 Android Studio 中测试互联网连接错误处理

标签 android android-espresso

我正在使用 Espresso 在模拟器上为 Android Studios 测试中的应用程序编写 Instrumentation 测试。我需要在测试中禁用互联网,以确保当用户没有可用互联网时我的错误处理功能可以正常工作。我到处寻找一种简单的方法来以编程方式从模拟设备禁用互联网。这是我最接近的,但不起作用。

        WifiManager wifiManager = (WifiManager) mActivityRule.getActivity().getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(false);

即使在@Before 代码块中调用它也没有任何作用,而且我还在 AndroidManifest.xml 文件中设置了必要的权限来更改 wifi 设置。

最佳答案

通过测试,我们可以轻松地在没有 root 访问权限的情况下运行命令:

InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("svc wifi disable")
InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("svc data disable")

我花了一些时间才找到它,所有这些答案和文章都提出了过时的解决方案(wifiManager.setWifiEnabled 在 API 29 中已弃用)

关于android - 使用 Espresso on Emulator 在 Android Studio 中测试互联网连接错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39759395/

相关文章:

javascript - 用户 ACTION_VIEW Intent 在 APP 中打开 URL

testing - 使用 gradle 进行双份 Espresso 测试 - 空测试套件

android-espresso - 如何将 Espresso 与 Kotlin Flow 结合使用?

android - 使用 Espresso 测试包含的 View

android - 错误 :(4, 32) 错误 : package android. hardware.camera2 不存在

android - Picasso 的 GridView 无法加载图像

android - 波纹效果不适用于图像的相对布局

android - 导入 'com.google.android.gms:play-services:7.3.0' 后运行应用程序时出现 Google Play 服务错误

android - 使用 Espresso 对随机列表元素执行单击

android - 使用 CountingIdlingResource 进行异步操作的仪器测试失败