How to turn on/off wifi, bluetooth, airplane mode on iOS by using Appium

These codes work well on iOS 10.3.3, appium 1.8.1

Firstly, launch new Appium instance with the following capabilities to open iOS Settings app (remember to replace your specific udid number):

var capabilities = DesiredCapabilities()
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "ios")
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iphone")
capabilities.setCapability(MobileCapabilityType.UDID, "udid")
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest")
capabilities.setCapability(IOSMobileCapabilityType.BUNDLE_ID, "com.apple.Preferences") // use bundleID to launch existed app

Then, do enable or disable wifi option:

//Element's info
static By wifiTab = MobileBy.xpath("//XCUIElementTypeCell[@visible='true' and @name='Wi-Fi']");
static By wifiBtn = MobileBy.xpath("//XCUIElementTypeSwitch[@visible='true' and @name='Wi-Fi']");
public static void enableWifi(boolean expectedWifiStatus) {
driver.findElement(wifiTab).click(); // To open Settings > Wi-fi
if (isWifiEnabled() != expectedWifiStatus) {
driver.findElement(wifiBtn).click(); // To switch Wi-fi
}
}

public static boolean isWifiEnabled() {
return driver.findElement(wifiBtn).getAttribute("value").equals("1"));
}

Enable or disable Bluetooth option:

static By bluetoothTab = MobileBy.xpath("//XCUIElementTypeCell[@visible='true' and @name='Bluetooth']");
static By bluetoothBtn = MobileBy.xpath("//XCUIElementTypeSwitch[@visible='true' and @name='Bluetooth']");
public static void enableBluetooth(boolean expectedBLEStatus) {
driver.findElement(bluetoothTab).click(); // To open Settings > Bluetooth
if (isBluetoothEnabled() != expectedBLEStatus) {
getDriver().findElement(bluetoothBtn).click(); // To switch Bluetooth
}
}

public static boolean isBluetoothEnabled() {
return driver.findElement(bluetoothBtn).getAttribute("value").equals("1");
}

Enable or disable Airplane mode option:

static By airplaneBtn = MobileBy.xpath("//XCUIElementTypeSwitch[@visible='true' and @name='Airplane Mode']");public static void enableAirplane(boolean expectedAirplaneStatus) {
if (isAirplaneModeEnabled() != expectedAirplaneStatus) {
getDriver().findElement(airplaneBtn).click(); // To switch Airplane Mode in Settings
}
}

public static boolean isAirplaneModeEnabled() {
return driver.findElement(airplaneBtn).getAttribute("value").equals("1");
}

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store