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.
Join the DZone community and get the full member experience.
Join For FreeIn 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.
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:
driver.toggleWifi();
On the Latest Android OS platform, you can not toggle Airplane mode by code. You will get an exception as below
xxxxxxxxxx
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.
xxxxxxxxxx
To disable Wifi and Data in Andorid:
driver.setConnection(new ConnectionStateBuilder()
.withWiFiDisabled()
.withDataDisabled()
.build());
To enable Wifi and Data Android:
driver.setConnection(new ConnectionStateBuilder()
.withWiFiEnabled()
.withDataEnabled()
.build());
(Note: Do not add selenium dependency in the project as otherwise, you will get an exception issue )
xxxxxxxxxx
Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.remote.http.HttpClient$Factory.createDefault()Lorg/openqa/selenium/remote/http/HttpClient$Factory;
3. Use ADB commands to Navigate Airplane Mode Menu and Toggle Airplane Mode:
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:
xxxxxxxxxx
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.
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:
x
import io.appium.java_client.MobileBy;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
import java.util.HashMap;
public class AirplaneModeiOSApp {
public static IOSDriver driver;
public static int expectedAirplaneStatus;
public static By airplaneBtn = MobileBy.xpath("//XCUIElementTypeSwitch[@name='Airplane Mode']");
public static By Allow = MobileBy.xpath("//XCUIElementTypeButton[@name='Allow']");
public static String settingsMenu="com.apple.Preferences";
public static String stock = "com.apple.stocks";
public static void main(String[]args) throws Exception {
HashMap<String, Object> param = new HashMap<>();
param.put(MobileCapabilityType.PLATFORM, "ios");
param.put(MobileCapabilityType.PLATFORM_VERSION, "13.4");
param.put(MobileCapabilityType.DEVICE_NAME, "iPhone");
param.put(MobileCapabilityType.UDID, "XXXXXXXXXXXX");
param.put(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
param.put("useNewWDA", false);
param.put(MobileCapabilityType.APP, stock);
DesiredCapabilities capabilities = new DesiredCapabilities(param);
driver = new IOSDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
try{
Thread.sleep(5000);
HashMap<String, Object> value = new HashMap<>();
value.put("bundleId", settingsMenu);
driver.executeScript("mobile: launchApp", value);
driver.executeScript("mobile: activateApp", value);
boolean status= enableAirplaneMode();
System.out.println(status);
value.put("bundleId", stock);
driver.executeScript("mobile: launchApp", value);
driver.executeScript("mobile: activateApp", value);
}
finally {
driver.quit();
}
}
public static boolean enableAirplaneMode(){
expectedAirplaneStatus=Integer.parseInt(driver.findElement(airplaneBtn).getAttribute("value"));
if(expectedAirplaneStatus==0){
driver.findElement(airplaneBtn).click();
System.out.println("Airplane mode is Turned On:");
return true;
}else{
System.out.println("Airplane mode is already On");
return false;
}
}
public static boolean disableAirplaneMode(){
expectedAirplaneStatus=Integer.parseInt(driver.findElement(airplaneBtn).getAttribute("value"));
if(expectedAirplaneStatus==1){
driver.findElement(airplaneBtn).click();
System.out.println("Airplane mode is Turned OFF:");
return true;
}else{
System.out.println("Airplane mode is already OFF");
return false;
}
}
}
Opinions expressed by DZone contributors are their own.
Comments