Using selenium and Appium is it possible to change between wifi networks?

孤街醉人 提交于 2019-12-04 15:04:37

you can toggle the wifi connection in Android (not possible in iOS) using the below appium commands,

To enable flight mode :

# Python

def enableFlightMode(self,context):
        driver.mobile.set_network_connection(driver.mobile.AIRPLANE_MODE)
        driver.implicitly_wait(10)
        if driver.network_connection == 1:
            self.report_pass("The network connection is disabled in the mobile and flight mode is active.")
        else:
            self.report_fail("The flight mode is not active yet!")

And to disable flight mode:

def enableFlightMode(self,context):
        driver.mobile.set_network_connection(driver.mobile.AIRPLANE_MODE)
        driver.implicitly_wait(10)
        if driver.network_connection == 1:
            self.report_pass("The network connection is disabled in the mobile and flight mode is active.")
        else:
            self.report_fail("The flight mode is not active yet!")
Darshan Ambhaikar

You can achieve this by accessing all available wifi networks using Shell Script. It means you need to write logic of firing ADB/Shell commands through your code.Connecting to wifi using adb shell

On your button click you can fire command to connect to desired wifi network.

Yes, it is possible to change the network using Appium. Look here

I am using below adb commands to turn on & off WiFi/data.And it is working fine.

Turn on wifi - adb shell am start -n io.appium.settings/.Settings -e wifi on

Turn off WiFi - adb shell am start -n io.appium.settings/.Settings -e wifi off

Turn on mobile data - adb shell am start -n io.appium.settings/.Settings -e data on

Turn off mobile data - adb shell am start -n io.appium.settings/.Settings -e data off

You can get/change network connection settings through AndroidDriver. However, it works only for Android version less than 5.

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();

This ADB command will certainly switch off your wifi :

adb shell am broadcast -a io.appium.settings.wifi --es setstatus disable

To turn it on use :

adb shell am broadcast -a io.appium.settings.wifi --es setstatus enable

OR

Try this code -

self.driver.open_notifications()
self.driver.find_element_by_xpath('//android.widget.Switch[@content-desc="Airplane mode"]').click()
self.driver.back()

Give me a thumps up if it works for you

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!