Swipe Element Using TouchAction Class In Android Appium Example

Earlier In previous post, nosotros learnt how to interact amongst android mobile gesture to perform horizontal together with vertical swipe using driver.swipe() inwards appium mobile software automation test. In THIS POST, We also learnt - how to perform drag together with drib inwards android app past times generating activity chain using TouchAction class. Here nosotros tin purpose same TouchAction cast to swipe exceptional chemical ingredient horizontally inwards android software app. Let's encounter how tin nosotros use TouchAction class to swipe element.

PREREQUISITES : All previous 20 steps of mobile software app's appium tutorials (PART 1 together with PART 2) should last completed.

Install SwipeListView Demo App
You tin sentiment my previous ship service to larn how to download together with install SwipeListView Demo software app inwards your android mobile device.

Aim To Achieve In This Appium Test
Our aim Is to swipe exceptional chemical ingredient inwards horizontal(Right to left together with and hence left to right) direction. As y'all tin encounter In bellow image, We wants to swipe quaternary chemical ingredient from listing grid of SwipeListView Demo app inwards horizontal(Right to left together with and hence left to right) direction.


Get element's Y axis coordinates
In gild to swipe chemical ingredient inwards X(horizontal) direction, You require to disclose element's seat inwards Y direction. To Get It,
  • Connect your mobile vociferation amongst PC amongst USB debugging trend enabled. VIEW MORE.
  • Open SwipeListView Demo app In mobile device.
  • Open UI Automator Viewer from tools folder of SDK. View THIS POST for to a greater extent than exceptional on UI Automator Viewer.
  • Capture device's screenshot In UI Automator Viewer.
  • Put mouse cursor at middle of quaternary element.
  • Note downward Y Coordinates every bit shown inwards bellow Image.


We volition purpose this Y coordinates In our appium exam script to perform swipe.

Create And Run Android Appium Test To Swipe Element
I convey created real uncomplicated exam script to swipe android app's chemical ingredient In horizontal direction. Create novel cast file SwipeAction.java under Android packet of your projection In eclipse together with re-create glue bellow given exam script In It.

Note : Please develop your device's capabilities inwards bellow given test.
SwipeAction.java
package Android;  import io.appium.java_client.MobileDriver; import io.appium.java_client.TouchAction; 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.Dimension; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test;  world cast SwipeAction {  AndroidDriver driver;  Dimension size;  WebDriverWait wait;  @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", "com.fortysevendeg.android.swipelistview");   capabilities.setCapability("appActivity","com.fortysevendeg.android.swipelistview.sample.activities.SwipeListViewExampleActivity");   driver = novel AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);   await = novel WebDriverWait(driver, 300);   wait.until(ExpectedConditions.elementToBeClickable(By.className("android.widget.RelativeLayout")));  }    @Test  world void swipingHorizontal() throws InterruptedException {   //Get the size of screen.   size = driver.manage().window().getSize();   System.out.println(size);      //Find swipe x points from screen's amongst together with height.   //Find x1 betoken which is at correct side of screen.   int x1 = (int) (size.width * 0.20);   //Find x2 betoken which is at left side of screen.   int x2 = (int) (size.width * 0.80);      //Create object of TouchAction class.   TouchAction activity = novel TouchAction((MobileDriver)driver);      //Find chemical ingredient to swipe from correct to left.   WebElement ele1 =  (WebElement) driver.findElementsById("com.fortysevendeg.android.swipelistview:id/front").get(3);     //Create swipe activity chain together with perform horizontal(right to left) swipe.   //Here swipe to betoken x1 Is at left side of screen. So It volition swipe chemical ingredient from correct to left.   action.longPress(ele1).moveTo(x1,580).release().perform();      //Find chemical ingredient to swipe from left to right.   WebElement ele2 =  (WebElement) driver.findElementsById("com.fortysevendeg.android.swipelistview:id/back").get(3);   //Create swipe activity chain together with perform horizontal(left to right) swipe.   //Here swipe to betoken x2 Is at correct side of screen. So It volition swipe chemical ingredient from left to right.   action.longPress(ele2).moveTo(x2,580).release().perform();  }   @AfterTest  world void End() {   driver.quit();  } }

Test Explanation
  • ele1 Is an chemical ingredient located to swipe from correct side to left side.
  • ele2 Is an chemical ingredient located to swipe from left side to correct side.
  • longPress() method volition press together with agree given element.
  • moveTo(X, Y) method volition motion chemical ingredient to given X together with Y coordinates.
  • release() method will remove the electrical flow touching implement from the screen.
  • perform() will perform this chain of actions on the driver.
Now foremost appium server together with operate exam script In eclipse together with disclose chemical ingredient swipe functioning inwards your android mobile device. This means y'all tin swipe specific chemical ingredient of android app using activity chain.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/

1 Comments

  1. I would say while reading your article i felt very proud,because the information you written very useful, please keep posting this type of articles. If you guys looking for a training institutes for Android Training in Bangalore. Please click below link.
    Android Institutes in Marathahalli

    ReplyDelete
Post a Comment
Previous Post Next Post