
There are situations where we need to toggle any settings to ON/OFF, e.g – switch ON/OFF wifi.
We can automate the toggle or switch ON/OFF using appium,

How we do manually – either click on the switch (on right side icon, as shown) or click on the text to switch ON/OFF.
If the color turns to green – then switch is ON
if color is grayed out, then switch is OFF
let’s see the implementation
public class SwitchOnOff {
static DesiredCapabilities cap;
static AndroidDriver driver;
@BeforeTest
public static void setup() throws MalformedURLException
{
cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PLATFORM, "Android");
cap.setCapability(CapabilityType.VERSION, "5.1.0");
cap.setCapability("deviceName", "mygeny510");
cap.setCapability("appPackage", "com.android.settings");
cap.setCapability("appActivity", ".Settings");
}
@Test
public static void SwitchONOFF() throws MalformedURLException
{
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
while(driver.findElements(By.name("Security")).size()==0)
{
Dimension dimensions = driver.manage().window().getSize();
Double screenHeightStart = dimensions.getHeight() * 0.5;
int scrollStart = screenHeightStart.intValue();
Double screenHeightEnd = dimensions.getHeight() * 0.2;
int scrollEnd = screenHeightEnd.intValue();
driver.swipe(0,scrollStart,0,scrollEnd,2000);
}
//driver.findElement(By.name("Security")).click();
driver.findElement(By.xpath("//*[@text='Security']")).click();
//clicking on name is same as clicking on the switch button
WebElement SwitchText = driver.findElement(By.name("Make passwords visible"));
SwitchText.click();
}
@AfterTest
public static void TearDown()
{
driver.quit();
}
}
Short link – bit.ly/qav-apmtoggleswitch




