{{announcement.body}}
{{announcement.title}}

Internet Offline Scenario Automation on Android and iOS Mobile App

DZone 's Guide to

Internet Offline Scenario Automation on Android and iOS Mobile App

In this article, we'll learn how to automate network connection offline scenarios using appium on the android and iOS platform and understand its importance.

· Web Dev Zone ·
Free Resource

In the world of digitization, the smartphone becomes the basic need of humans. More than 3 billion users are using smartphones across the globe. Whereas more than a million mobile applications present on Google Play Store, Apple App Store, and other application stores. The number of developers is developing mobile applications whereas Software Tester is involved in assuring the quality of Mobile App using manual or by doing automation.

No internet android and chrome

As Software tester verifying and delivering the quality mobile application is a cumbersome task, the reason behind this device fragmentation in the smartphone, device screen size,  hardware dependency, Operating system, and verifying network-related scenario.  

Here we are going to learn how to automate network connection offline scenario using appium on android and ios platform and its importance. As we all know mobile applications behave unusually when we move to a slow network or out of the network. I think we all have experienced it in the subway, underground metro, parking lot, or elevator. Some times mobile application get crashed due to poor handling of network scenarios may lead to the potential of the customer as well. The best way to avoid this test a mobile application using real comprehensive scenario testing.

Appium is a widely used open-source mobile application automation tool. I used Java and Appium for automation purposes so you need a prior understanding of Appium and basics of Java language.

Turn Off the Network Connection in Android Device:

1. Toggle Wi-Fi in Android Network if Mobile Network is Not Available:

Java
 




x


1
driver.toggleWifi();



On the Latest Android OS platform, you can not toggle Airplane mode by code. You will get an exception as below

Java
 




xxxxxxxxxx
1


 
1
shell am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true' exited with code 255'; Stderr: 'Security exception: Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE from pid=9631, uid=2000



2. To achieve this on Appium Java Client  7.2.0 above version you can use the below code.

Java
 




xxxxxxxxxx
1
12


1
To disable Wifi and Data in Andorid:
2
driver.setConnection(new ConnectionStateBuilder()
3
                     .withWiFiDisabled()
4
                     .withDataDisabled()
5
                     .build());
6
 
          
7
To enable Wifi and Data Android:
8
driver.setConnection(new ConnectionStateBuilder()
9
                     .withWiFiEnabled()
10
                     .withDataEnabled()
11
                     .build());
12
 
          


(Note: Do not add selenium dependency in the project as otherwise, you will get an exception issue )

Java
 




xxxxxxxxxx
1


1
Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.remote.http.HttpClient$Factory.createDefault()Lorg/openqa/selenium/remote/http/HttpClient$Factory;
2
 
          


3. Use ADB commands to Navigate Airplane Mode Menu and Toggle Airplane Mode:

Java
 




x


 
1
adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS



Or You can navigate to Wifi Settings Of Android Device And Toggle Setting using Xpath of Locator:

Java
 




xxxxxxxxxx
1


 
1
adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings



Internet Connection Offline Scenario Automation in iOS devices:

   You can toggle Wi-Fi or data connection in Android devices using ADB commands or Appium in build commands. But in the case of the iOS platform, Appium does not provide direct commands for making internet connection offline. 

iOS mobile automation

If you are new in iOS application automation, then you might want to read the below article:

Appium v 1.7.1 setup on Mac OS Sierra for iOS Real device automation.

You can enable Airplane mode in the iOS device using Appium and by getting native app bundle id. You can get here the default bundle id list for iOS application according to the OS version. 

In the below code, we used two default bundle id one for the stock app and settings of the iOS device.

We simply switched app using the bundle identifier and performed Appium commands for enabling and disable Airplane Mode.

Here is the sample code snippet for enabling and Disable Airplane Mode in iOS:

Java
 




x


 
1
import io.appium.java_client.MobileBy;
2
import io.appium.java_client.ios.IOSDriver;
3
import io.appium.java_client.remote.MobileCapabilityType;
4
import org.openqa.selenium.By;
5
import org.openqa.selenium.remote.DesiredCapabilities;
6
import java.net.URL;
7
import java.util.HashMap;
8
public class AirplaneModeiOSApp {
9
    public static IOSDriver driver;
10
    public static int expectedAirplaneStatus;
11
    public static By airplaneBtn = MobileBy.xpath("//XCUIElementTypeSwitch[@name='Airplane Mode']");
12
    public static By Allow = MobileBy.xpath("//XCUIElementTypeButton[@name='Allow']");
13
    public static String settingsMenu="com.apple.Preferences";
14
    public static String stock = "com.apple.stocks";
15
    public static void main(String[]args) throws Exception {
16
        HashMap<String, Object> param = new HashMap<>();
17
        param.put(MobileCapabilityType.PLATFORM, "ios");
18
        param.put(MobileCapabilityType.PLATFORM_VERSION, "13.4");
19
        param.put(MobileCapabilityType.DEVICE_NAME, "iPhone");
20
        param.put(MobileCapabilityType.UDID, "XXXXXXXXXXXX");
21
        param.put(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
22
        param.put("useNewWDA", false);
23
        param.put(MobileCapabilityType.APP, stock);
24
        DesiredCapabilities capabilities = new DesiredCapabilities(param);
25
        driver = new IOSDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
26
        try{
27
            Thread.sleep(5000);
28
            HashMap<String, Object> value = new HashMap<>();
29
            value.put("bundleId", settingsMenu);
30
            driver.executeScript("mobile: launchApp", value);
31
            driver.executeScript("mobile: activateApp", value);
32
            boolean status=  enableAirplaneMode();
33
            System.out.println(status);
34
            value.put("bundleId", stock);
35
            driver.executeScript("mobile: launchApp", value);
36
            driver.executeScript("mobile: activateApp", value);
37
        }
38
        finally {
39
            driver.quit();
40
        }
41
    }
42
 
          
43
    public static boolean enableAirplaneMode(){
44
        expectedAirplaneStatus=Integer.parseInt(driver.findElement(airplaneBtn).getAttribute("value"));
45
        if(expectedAirplaneStatus==0){
46
            driver.findElement(airplaneBtn).click();
47
            System.out.println("Airplane mode is Turned On:");
48
            return true;
49
        }else{
50
            System.out.println("Airplane mode is already On");
51
            return false;
52
        }
53
    }
54
    public static boolean disableAirplaneMode(){
55
        expectedAirplaneStatus=Integer.parseInt(driver.findElement(airplaneBtn).getAttribute("value"));
56
        if(expectedAirplaneStatus==1){
57
            driver.findElement(airplaneBtn).click();
58
            System.out.println("Airplane mode is Turned OFF:");
59
            return true;
60
        }else{
61
            System.out.println("Airplane mode is already OFF");
62
            return false;
63
        }
64
    }
65
}



Topics:
appium testing, mobile automation testing

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}