Espresso:切换网络状态

时间:2018-09-21 20:00:52

标签: android kotlin network-programming android-espresso

我目前正在研究一种Espresso测试套件,该套件涵盖离线功能。为了实现这些测试,我需要创建一个可以调用以打开/关闭网络连接的方法。到目前为止,我已经能够切换WiFi,但是我还无法弄清楚如何关闭蜂窝数据。

任何信息将不胜感激。

1 个答案:

答案 0 :(得分:0)

此解决方案对我有用: https://sqa.stackexchange.com/questions/23646/how-can-i-switch-on-off-airplane-mode-and-wifi-using-appium?answertab=votes#tab-top

您还可以执行 really 垃圾解决方法。 注意:我这样做主要是出于娱乐目的,不促进其实际使用;)

#!/bin/bash

### SET Airplane Mode ON ###
adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS \
&& for i in {1..5}
do 
    adb shell input keyevent 20
done \
&& adb shell input keyevent 23 \
&& adb shell input keyevent 4;

### Run tests ###

### SET Airplane Mode OFF ###
# NOTE: When Airplane Mode is enabled in API 28, "Mobile network" is disabled. Additionally, since Android Setting's Network & internet 
# is still running in the background, you'll have to select the down action two less times. 
adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS \
&& for i in {1..3}
do 
    adb shell input keyevent 20
done \
&& adb shell input keyevent 23 \
&& adb shell input keyevent 4;