Appium Concepts : Enabling & Disabling “Mobile Data” via Appium(Built-In Method) at Runtime
Appium Built-in Method: “.toggleData()”

Get a step-by-step walkthrough in the video below!
Inthis Article, We will discuss how to Enable & Disable “Mobile Data” via Appium(Built-In Method) at Runtime
Appium Built-In Method : — “.toggleData()”
Description:
Toggles the mobile data connection on a device.
If mobile data is currently enabled, this function disables it; if it is disabled, this function enables it.
Returns:
- true if the mobile data state was successfully toggled.
- false or an error/exception if the operation failed (e.g., due to permission issues or unsupported platform).
Example Usage:
device.toggleData(); // Switches mobile data on or off depending on the current state
“Complete Code: Optimized and Ready to Use”:
package com.appiumguide.appiumconcepts;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
/**
* Script Details - Appium Concepts : Enabling & Disabling “Data” via Appium(Built-In Method) at Runtime
*
* appium-java-client version: Latest
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/
public class ChangeDataStateDemo {
private AndroidDriver driver;
@BeforeTest
public void setup() throws MalformedURLException {
UiAutomator2Options cap=new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setDeviceName("Pixel8");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), cap);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(100));
}
@Test
public void test() throws InterruptedException, IOException {
//Enable or disable the mobile data
//Disable the mobile data(if already it is enabled)
driver.toggleData();
System.out.println("Mobile data is disabled");
Thread.sleep(5000);
//Enable the mobile data(if it is disabled)
driver.toggleData();
System.out.println("Mobile data is enabled");
}
@AfterTest
public void teardown() {
if(driver!=null) {
driver.quit();
System.out.println("Test Completed");
}
}
}
Notes:
- This function typically requires elevated permissions or root access, especially on Android.
- iOS generally does not allow programmatic control over mobile data for third-party apps due to security and privacy policies.
- Availability and behavior can vary depending on the device manufacturer and OS version.
GitHub Link:
No comments:
Post a Comment