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

In android, There is 1 of import chemical cistron ON/OFF switch. It is rattling slowly to ON/OFF switch inward android automation test using appium but y'all should know how to create it. In this article, I am going to described y'all how to ON/OFF switch inward appium android automation test using tap(int fingers, int duration) method of MobileElement class. Also y'all tin piece of occupation click() method to tap on push clitoris but hither nosotros volition piece of occupation tap method for your understanding.

App To Use
We volition piece of occupation API Demos app inward this test. You tin download it from google play.

Aim Of Test
In this appium automation test, We wants to tap on Monitored switch "OFF" push clitoris to switch "ON" it in addition to and thus 1 time once again tap on Monitored switch "ON" push clitoris to switch "OFF" it every bit shown inward bellow image. Also nosotros wants to verify that switch is ON or OFF before taping on it.


In API Demos app, You tin manually navigate to to a higher house concealment from App Home -> Views -> Switches.

Create And Run Test
Create bellow given examination inward eclipse in addition to run it. View before published APPIUM TUTORIALS if y'all 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;  populace degree TapOnSwitch {  AndroidDriver driver;   @BeforeTest  populace void setUp() throws Exception {   DesiredCapabilities capabilities = novel 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 = novel AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);  }   @Test  populace void tapSwitchOnOff() throws InterruptedException {   // Scroll till chemical cistron which contains "Views" text If It Is non visible on screen.   driver.scrollTo("Views");   // Click on Views.   driver.findElement(By.name("Views")).click();   // Scroll till chemical cistron which contains "Switches" text If It Is non visible on screen.   driver.scrollTo("Switches");   // Click on Switches.   driver.findElement(By.name("Switches")).click();      //Get condition 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 push clitoris chemical cistron past times get upward = Monitored switch OFF.    MobileElement swt = (MobileElement) driver.findElementByName("Monitored switch OFF");    //Tap on switch push clitoris "Monitored switch OFF" to become far ON.    swt.tap(1, 1);    System.out.println("Monitored switch is ON now.");   }   //Pause examination for v seconds to seat switch ON for v seconds.   Thread.sleep(5000);      //Get condition 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 push clitoris chemical cistron past times get upward = Monitored switch ON.    MobileElement swt = (MobileElement) driver.findElementByName("Monitored switch ON");    //Tap on switch push clitoris "Monitored switch OFF" to become far OFF.    swt.tap(1, 1);    System.out.println("Monitored switch is OFF now.");   }  }    @AfterTest  populace void End() {   driver.quit();  } }

When y'all run to a higher house test,
  • It volition open API Demos app inward android device.
  • Navigate to Switches screen.
  • Tap on Monitored switch to switch it ON from OFF.
  • Once 1 time again tap on Monitored switch to switch it OFF from ON.
This agency y'all tin tap on whatever switch of android using tap method of MobileElement class.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post