1

How can I switch ON/OFF AIRPLANE mode and wifi using Appium ?

Selenium has below option:

NetworkConnection mobileDriver = (NetworkConnection) driver;
 if (mobileDriver.getNetworkConnection() != ConnectionType.AIRPLANE_MODE) {
   // enabling Airplane mode
   mobileDriver.setNetworkConnection(ConnectionType.AIRPLANE_MODE);
 }

But when I create AndroidDriver object and typecast it with above code, getting error that such typecast is not possible.

Any other way ?

1
  • We do it using a small custom app that appium runs, I don't know why it doesn't work.
    – Rsf
    Nov 15, 2016 at 9:15

5 Answers 5

3

This helper method will help you to turn on the wifi while your test case is still running:

static AppiumDriver driver;    

public static void wifiOff() throws InterruptedException {
    NetworkConnection mobileDriver = (NetworkConnection) driver;
    if (mobileDriver.getNetworkConnection() != ConnectionType.AIRPLANE_MODE) {
       // enabling Airplane mode
       mobileDriver.setNetworkConnection(ConnectionType.AIRPLANE_MODE);
    }
}

Works for me.

0
0

Try using hardware keys. Write the steps to turn it on and off by using hardware buttons.

1
  • this will only work until the next OS update and change of menu structure
    – Rsf
    Nov 15, 2016 at 9:16
0

According to the Appium Java API docs you'd do something like this (I haven't tested it):

driver.setConnection(Connection.AIRPLANE);

Note that only the Android driver supports this (not the iOS driver). See Adjusting Network Connection.

0

Currently you can switch between ON/OFF options of Airplane mode only in Android. There is still no option to do that on iOS. This being said, this is what you would do to enable/disable airplane mode in Android.

((AndroidDriver) DRIVER).setConnection(Connection.AIRPLANE);
boolean connectionStatus = ((AndroidDriver) DRIVER)
    .getConnection()
    .equals(Connection.AIRPLANE);
if (connectionStatus) {
    //Then do something
}
0

In Appium Latest Java client you can do it using To disable Wifi and Data in Android: driver.setConnection(new ConnectionStateBuilder().withWiFiDisabled().withDataDisabled().build());

To enabled WiFI and Data in Android: driver.setConnection(new ConnectionStateBuilder().withWiFiEnabled().withDataEnabled().build());

To Enable and Disable Airplane mode in iOS you can refer below article: Internet Offline Scenario Automation on Android and iOS Mobile App

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.