Appium : Tap On Switch Button Of Android App To ON/OFF

In android, There is one important element ON/OFF switch. It is very easy to ON/OFF switch in android automation test using appium but you should know how to do it. In this article, I am going to described you how to ON/OFF switch in appium android automation test using tap(int fingers, int duration) method of MobileElement class. Also you can use click() method to tap on button but here we will use tap method for your understanding.

App To Use
We will use API Demos app in this test. You can download it from google play.

Aim Of Test
In this appium automation test, We wants to tap on Monitored switch "OFF" button to switch "ON" it and then once again tap on Monitored switch "ON" button to switch "OFF" it as shown in bellow image. Also we wants to verify that switch is ON or OFF before taping on it.


In API Demos app, You can manually navigate to above screen from App Home -> Views -> Switches.

Create And Run Test
Create bellow given test in eclipse and run it. View earlier published APPIUM TUTORIALS if you don't know how to run appium test.

TapOnSwitch.java
package Android;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TapOnSwitch {
 AndroidDriver driver;

 @BeforeTest
 public void setUp() throws Exception {
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "ZX1B32FFXF");
  capabilities.setCapability("browserName", "Android");
  capabilities.setCapability("platformVersion", "4.4.2");
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("appPackage", "io.appium.android.apis");
  capabilities.setCapability("appActivity", "io.appium.android.apis.ApiDemos");
  driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 }

 @Test
 public void tapSwitchOnOff() throws InterruptedException {
  // Scroll till element which contains "Views" text If It Is not visible on screen.
  driver.scrollTo("Views");
  // Click on Views.
  driver.findElement(By.name("Views")).click();
  // Scroll till element which contains "Switches" text If It Is not visible on screen.
  driver.scrollTo("Switches");
  // Click on Switches.
  driver.findElement(By.name("Switches")).click();
  
  //Get status of switch using it's text.
  String switchStatus1 = driver.findElementById("io.appium.android.apis:id/monitored_switch").getText();
  System.out.println(switchStatus1);
  //If switchStatus1 = Monitored switch OFF, Execute inner code.
  if((switchStatus1.trim()).equals("Monitored switch OFF")){
   System.out.println("Doing Monitored switch ON");
   //Locate switch button element by name = Monitored switch OFF.
   MobileElement swt = (MobileElement) driver.findElementByName("Monitored switch OFF");
   //Tap on switch button "Monitored switch OFF" to make it ON.
   swt.tap(1, 1);
   System.out.println("Monitored switch is ON now.");
  }
  //Pause test for 5 seconds to put switch ON for 5 seconds.
  Thread.sleep(5000);
  
  //Get status of switch using it's text.
  String switchStatus2 = driver.findElementById("io.appium.android.apis:id/monitored_switch").getText();
  System.out.println(switchStatus2);
  //If switchStatus1 = Monitored switch ON, Execute inner code.
  if((switchStatus2.trim()).equals("Monitored switch ON")){
   System.out.println("Doing Monitored switch OFF");
   //Locate switch button element by name = Monitored switch ON.
   MobileElement swt = (MobileElement) driver.findElementByName("Monitored switch ON");
   //Tap on switch button "Monitored switch OFF" to make it OFF.
   swt.tap(1, 1);
   System.out.println("Monitored switch is OFF now.");
  }
 }
 
 @AfterTest
 public void End() {
  driver.quit();
 }
}

When you run above test,
  • It will open API Demos app in android device.
  • Navigate to Switches screen.
  • Tap on Monitored switch to switch it ON from OFF.
  • Once again tap on Monitored switch to switch it OFF from ON.
This way you can tap on any switch of android using tap method of MobileElement class.

No comments:

Post a Comment