Appium Concepts : Enabling & Disabling “Wifi” via Appium(Built-In Method) at Runtime
Appium Built-in Method: “.toggleWifi()”

Get a step-by-step walkthrough in the video below!
Inthis Article, We will discuss how to Enable & Disable “Wifi” via Appium(Built-In Method) at Runtime
Appium Built-In Method : — “.toggleWifi()”
Description:
Toggles the Wi-Fi state on a device. If Wi-Fi is currently enabled, this function disables it.
If Wi-Fi is currently disabled, this function enables it.
Returns:
- true or a success indicator if the operation completes successfully.
- false or an error/exception if the operation fails or is unsupported.
Basic Syntax:
device.toggleWifi(); // Turns Wi-Fi on if it's off, or off if it's on“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 “Wifi” via Appium(Built-In Method) at Runtime
*
* appium-java-client version: Latest
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/
public class ChangeWifiStateDemo {
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 Wifi State at runtime
//Disable the Wifi State(if already enabled)
driver.toggleWifi();
System.out.println("Wifi State is disabled");
Thread.sleep(5000);
//Enabled the Wifi State(if wifi is disabled
driver.toggleWifi();
System.out.println("Wifi State got enabled");
}
@AfterTest
public void teardown() {
if(driver!=null) {
driver.quit();
System.out.println("Test Completed");
}
}
}Note:
- The exact implementation and availability of .toggleWifi() depend on the platform or API (e.g., Android, custom JavaScript environments, automation libraries).
- May require special permissions depending on the environment (e.g., system-level permissions on Android).
GitHub Link:
No comments:
Post a Comment