【问题标题】:How to get/set network connection through Appium for an android device如何通过 Appium 为 Android 设备获取/设置网络连接
【发布时间】:2015-09-27 02:29:54
【问题描述】:

我尝试使用 Appium 来测试我的 android 设备: 我需要做的基本工作是获取网络连接状态。 环境:[Appium Server v 1.4.0 on Windows 7 + Python Client 0.16]

from appium import webdriver

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4'
desired_caps['deviceName'] = 'SamSung Galaxy Note4'
desired_caps['browserName'] = 'Chrome'


driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.get('http://google.com')
print driver.contexts
print driver.network_connection
time.sleep(5)
driver.quit()

但是,当我运行脚本时,我得到了以下输出:

> [u'NATIVE_APP', u'WEBVIEW_1'] Traceback (most recent call last):  
> File "D:/PycharmProjects/Work/hello/Work_YM/Appium/sample_1.py", line
> 15, in <module>
>     print driver.network_connection()   File "C:\Python27\lib\site-packages\appium\webdriver\webdriver.py", line
> 601, in network_connection
>     return self.execute(Command.GET_NETWORK_CONNECTION, {})['value']   File
> "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py",
> line 195, in execute
>     self.error_handler.check_response(response)   File "C:\Python27\lib\site-packages\appium\webdriver\errorhandler.py", line
> 29, in check_response
>     raise wde selenium.common.exceptions.WebDriverException: Message: "unknown command:
> session/31fe15845b2580877c1900c31d8f688d/network_connection"

似乎network_connection(这是webDriver的一个属性)不起作用,任何人都可以提供一些帮助吗? 谢谢!

【问题讨论】:

  • 我没有找到通过 Appium 获取/设置网络设置的方法。使用另一种方式:adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings &lt;br/&gt; adb shell input keyevent 19 &amp; adb shell input keyevent 23

标签: android python wifi appium


【解决方案1】:

您可以通过AndroidDriver 获取/更改网络连接设置。但是,它仅适用于低于 5 的 Android 版本。

AppiumDriver<WebElement> driver = new AndroidDriver<WebElement>(new URL("..."), caps);
NetworkConnectionSetting networkConnection = new NetworkConnectionSetting(false, true, false);  // airplane mode, wiif, data

networkConnection.setData(true);  // enable mobile data
networkConnection.setWifi(false); // close wifi

((AndroidDriver<WebElement>)driver).setNetworkConnection(networkConnection);
networkConnection = ((AndroidDriver<WebElement>)driver).getNetworkConnection();

【讨论】:

    【解决方案2】:

    以下代码可能会解决您的问题。

    NetworkConnectionSetting ncs = new NetworkConnectionSetting(false, true, false);
    driver.setNetworkConnection(ncs);
    System.out.println(ncs.toString());
    
    ncs.setWifi(false);
    driver.setNetworkConnection(ncs);
    System.out.println(ncs.toString());
    

    【讨论】:

    • 欢迎来到 SO!请编辑您的答案,以便它有一些与问题相关的介绍以及一些解释它是如何工作/帮助的。仅代码的答案通常没有太大帮助,您的代码甚至没有在里面。
    猜你喜欢
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2016-03-25
    • 1970-01-01
    • 2011-10-31
    • 2012-02-10
    • 1970-01-01
    相关资源
    最近更新 更多