简体   繁体   English

使用Robotium关闭Internet连接

[英]Turning Internet Connectivity off with Robotium

如果应用本身无法访问,是否可以通过Robotium打开/打开互联网连接(数据/ Wi-Fi)?

It's not possible in direct way without permission to do it. 未经许可,无法以直接方式进行操作。

You can create another application with that permission, this application should have implemented broadcast receiver for getting broadcast messages for turning on/off wifi and this way you can send broadcast messages from your android test to this application. 您可以创建具有该权限的另一个应用程序,该应用程序应该已经实现了广播接收器,以获取用于打开/关闭wifi的广播消息,这样您就可以将android测试中的广播消息发送到此应用程序。

It is possible to do it without any permissions (Tested on API 20). 可以在没有任何权限的情况下进行操作(已在API 20上测试)。 I will not write the code here as you would end up getting to realize one thing... As soon as you disables data (aka network) the emulator gets disconnected and your tests fail. 我不会在此处编写代码,因为您最终会意识到一件事...禁用数据(又称为网络)后,模拟器将断开连接,并且测试失败。 It seems that it's a bug. 看来这是一个错误。 Maybe it's not a problem on Genymotion. 也许这不是Genymotion的问题。 I haven't tried it. 我还没试过

If you'd like to try that I am not kidding do this: 如果您想尝试一下我不是在开玩笑,请执行以下操作:

  1. start your app in debug mode 在调试模式下启动您的应用
  2. stop on a breakpoint 在断点处停止
  3. from command line issue adb shell svc data disable 从命令行发布adb shell svc data disable
  4. now all tests should be down. 现在所有测试都应该关闭。

In general the solution that does not require any permission is calling the adb from the java code. 通常,不需要任何权限的解决方案是从Java代码中调用adb。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM