Wednesday, September 5, 2018

Appium : Tap On Switch Push Of Android App To On/Off

| Wednesday, September 5, 2018
In android, There is 1 of import chemical ingredient ON/OFF switch. It is real slowly to ON/OFF switch inwards android automation test using appium but you lot should know how to produce it. In this article, I am going to described you lot how to ON/OFF switch inwards appium android automation test using tap(int fingers, int duration) method of MobileElement class. Also you lot tin dismiss purpose click() method to tap on push clit but hither nosotros volition purpose tap method for your understanding.

App To Use
We volition purpose API Demos app inwards this test. You tin dismiss download it from google play.

Aim Of Test
In this appium automation test, We wants to tap on Monitored switch "OFF" push clit to switch "ON" it too thence 1 time once again tap on Monitored switch "ON" push clit to switch "OFF" it equally shown inwards bellow image. Also nosotros wants to verify that switch is ON or OFF before taping on it.


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

Create And Run Test
Create bellow given examination inwards eclipse too run it. View before published APPIUM TUTORIALS if you lot 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;  world degree TapOnSwitch {  AndroidDriver driver;   @BeforeTest  world 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  world void tapSwitchOnOff() throws InterruptedException {   // Scroll till chemical ingredient 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 ingredient 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 clit chemical ingredient yesteryear mention = Monitored switch OFF.    MobileElement swt = (MobileElement) driver.findElementByName("Monitored switch OFF");    //Tap on switch push clit "Monitored switch OFF" to arrive ON.    swt.tap(1, 1);    System.out.println("Monitored switch is ON now.");   }   //Pause examination for five seconds to seat switch ON for five 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 clit chemical ingredient yesteryear mention = Monitored switch ON.    MobileElement swt = (MobileElement) driver.findElementByName("Monitored switch ON");    //Tap on switch push clit "Monitored switch OFF" to arrive OFF.    swt.tap(1, 1);    System.out.println("Monitored switch is OFF now.");   }  }    @AfterTest  world void End() {   driver.quit();  } }

When you lot run inwards a higher house test,
  • It volition open API Demos app inwards 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 means you lot tin dismiss tap on whatsoever switch of android using tap method of MobileElement class.

Related Posts