Using: selenium webdriver, rubygems, appium, android and ios devices
The app I am testing has a button which becomes enabled only when connected to a specific wireless network. I'd like to create a script which will check if the button is active or not
vKioskStatus = element.enabled?
puts(vKioskStatus)
If it is not active, then I'd like to change wifi networks. Is that possible to automate changing networks on a mobile device?
UPDATE
I'm receiving the following error when trying to use getNetworkConnection. Is there a require I need to add?
<main>': undefined method `getNetworkConnection' for #<Selenium::WebDriver::Driver:0x..fe1a5511e browser=:firefox> (NoMethodError)
Here's my code:
require 'rubygems'
require 'selenium-webdriver'
require 'uri'
require 'appium_lib'
require_relative 'AndroidLib'
cButton = Buttons.new
driver = Selenium::WebDriver.for(:remote, :url => "http://127.0.0.1:4723/wd/hub") # Works for Android
sleep(5)
bob = driver.getNetworkConnection()
puts bob
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!")
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