buttons

 

Appium Concepts: How to Simulate “NetworkSpeed” on Android Emulators with Appium — In 3 ways

🎥 Check Out the Video Below for Complete Details!

Testing mobile apps under various network conditions is crucial to ensure optimal performance and a smooth user experience. Slow, unstable, or fluctuating networks can reveal issues that are not visible under ideal conditions. By Appium, we can simulate different network speeds on Android emulators to test such scenarios.

In this article, we’ll walk through how to simulate network speed changes on Android emulators using Appium.

Here Are 3 Smart Ways to Get It Done:

  1. By Using ‘Desired Capabilities’
  2. By Using ‘CommandLine’
  3. By Using ‘UIAutomator2 drivers’ Command

Available Network Speeds:

gsm — GSM/CSD (up: 14.4, down: 14.4).

hscsd — HSCSD (up: 14.4, down: 57.6).

gprs — GPRS (up: 28.8, down: 57.6).

edge — EDGE/EGPRS (up: 473.6, down: 473.6).

umts — UMTS/3G (up: 384.0, down: 384.0).

hsdpa — HSDPA (up: 5760.0, down: 13,980.0).

lte — LTE (up: 58,000, down: 173,000).

evdo — EVDO (up: 75,000, down: 280,000).

full — No limit, the default (up: 0.0, down: 0.0).

num — Specifies both upload and download speed.

up:down — Specifies individual up and down speeds.

Supported Capability — “networkSpeed”

This capability allows you to specify the initial network speed for an Android emulator when the session is created.

UiAutomator2Options cap=new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setDeviceName("Pixel8Pro");
cap.setNetworkSpeed("lte");

or

cap.setCapability("networkSpeed","lte"); //lte

⚠️ Important: This only works with Android Emulators, not real devices.

Syntax:

emulator -avd <emulator-name> -netspeed <speed>

or

adb -s <emulator-name> emu network speed <speed>

Example:

emulator -avd Pixel8Pro -netspeed lte

Command:

Example:

driver.executeScript("mobile: networkSpeed",ImmutableMap.of("speed","lte"));
or
driver.executeScript("mobile: networkSpeed",ImmutableMap.of("speed","gsm"));
or
driver.executeScript("mobile: networkSpeed",ImmutableMap.of("speed","full"));
or
driver.executeScript("mobile: networkSpeed",ImmutableMap.of("speed","edge"));
or
driver.executeScript("mobile: networkSpeed",ImmutableMap.of("speed","evdo"));

package demo;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import java.net.*;
import java.time.Duration;

/**
* Script Details - Appium Concepts | How to Simulate “NetworkSpeed” on Android Emulators with Appium
*
* appium-java-client version: 9.3.0
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/


public class NetworkSpeedDemo {

private AndroidDriver driver;

@BeforeTest
public void setup() throws MalformedURLException {
UiAutomator2Options cap=new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setDeviceName("Pixel8Pro");
cap.setNetworkSpeed("lte");
//or
cap.setCapability("networkSpeed","lte"); //lte
cap.setCapability("networkSpeed","gsm"); //gsm
cap.setCapability("networkSpeed","egde"); //edge
cap.setCapability("networkSpeed","full"); //full
cap.setCapability("networkSpeed","gprs"); //gprs


driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), cap);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

}

@Test
public void test() {

driver.executeScript("mobile: networkSpeed",ImmutableMap.of("speed","lte"));
driver.executeScript("mobile: networkSpeed",ImmutableMap.of("speed","gsm"));
driver.executeScript("mobile: networkSpeed",ImmutableMap.of("speed","full"));
driver.executeScript("mobile: networkSpeed",ImmutableMap.of("speed","edge"));
driver.executeScript("mobile: networkSpeed",ImmutableMap.of("speed","evdo"));

}


@AfterTest
public void teardown() {

if(driver!=null) {
driver.quit();
System.out.println("Test Execution Completed");

}

}

}

🚫 Limitations:

  • This approach only works on emulators, not physical devices.
  • Emulator must be running and accessible via ADB.
  • Network latency cannot be directly configured with this command (use emu network delay for that).

Explore on GitHub:


No comments:

Post a Comment