'How to disable wi-fi on Android device in Appium?
In our test case: I need to disable Wi-Fi at some specific point/action.
I have verified:
driver[deviceIndex].setConnection(Connection.NONE);
assertEquals(Connection.ALL, driver[deviceIndex].getConnection());
But it's not doing anything.
My expectation is "turning OFF wi-fi" and keeping Mobile Data ON.
Current code is doing no action.
Solution 1:[1]
You need to add Apache common langs to your project. https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.9
Solution 2:[2]
In appium you can only toggle the wifi network using driver.toggleWifi() method. That means it will change wifi to off state if it is in on state and vice versa. To use toggleWifi() method you must use androidDriver. If you are using AppiumDriver you can typecast it to AndroidDriver like following.
((AndroidDriver) driver).toggleWifi();
You can also use setNetworkConnection() method but it also has certain limitaions. The limitations are:
Solution 3:[3]
What is the type of driver you are using? You should use AndroidDriver if you are working on Android. Here are the working code for me.
// turn on all (data and wi-fi)
public void turnOnAllData() {
driver.setConnection(Connection.ALL);
}
// turn off all (data and wi-fi)
public void turnOffAllData() {
driver.setConnection(Connection.NONE);
}
// turn on airplane
public void turnOnAirplaneMode() {
driver.setConnection(Connection.AIRPLANE);
}
// turn on data
public void turnOnMobileData() {
driver.setConnection(Connection.DATA);
}
// turn on wi-fi
public void turnOnWiFi() {
driver.setConnection(Connection.WIFI);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sharaf |
Solution 2 | Suban Dhyako |
Solution 3 | Rajesh Chaudhary |