Swipe Element Using TouchAction Class In Android Appium Example

Earlier In previous post, we learnt how to interact with android mobile gesture to perform horizontal and vertical swipe using driver.swipe() in appium mobile software automation test. In THIS POST, We also learnt - how to perform drag and drop in android app by generating action chain using TouchAction class. Here we can use same TouchAction class to swipe particular element horizontally in android software app. Let's see how can we use TouchAction class to swipe element.

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

Install SwipeListView Demo App
You can view my previous post to learn how to download and install SwipeListView Demo software app in your android mobile device.

Aim To Achieve In This Appium Test
Our aim Is to swipe particular element in horizontal(Right to left and then left to right) direction. As you can see In bellow image, We wants to swipe 4th element from listing grid of SwipeListView Demo app in horizontal(Right to left and then left to right) direction.


Get element's Y axis coordinates
In order to swipe element in X(horizontal) direction, You need to find element's position in Y direction. To Get It,
  • Connect your mobile phone with PC with USB debugging mode enabled. VIEW MORE.
  • Open SwipeListView Demo app In mobile device.
  • Open UI Automator Viewer from tools folder of SDK. View THIS POST for more detail on UI Automator Viewer.
  • Capture device's screenshot In UI Automator Viewer.
  • Put mouse cursor at middle of 4th element.
  • Note down Y Coordinates as shown in bellow Image.


We will use this Y coordinates In our appium test script to perform swipe.

Create And Run Android Appium Test To Swipe Element
I have created very simple test script to swipe android app's element In horizontal direction. Create new class file SwipeAction.java under Android package of your project In eclipse and copy paste bellow given test script In It.

Note : Please set your device's capabilities in 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;

public class SwipeAction {
 AndroidDriver driver;
 Dimension size;
 WebDriverWait wait;
 @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", "com.fortysevendeg.android.swipelistview");
  capabilities.setCapability("appActivity","com.fortysevendeg.android.swipelistview.sample.activities.SwipeListViewExampleActivity");
  driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  wait = new WebDriverWait(driver, 300);
  wait.until(ExpectedConditions.elementToBeClickable(By.className("android.widget.RelativeLayout")));
 }


 @Test
 public 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 with and height.
  //Find x1 point which is at right side of screen.
  int x1 = (int) (size.width * 0.20);
  //Find x2 point which is at left side of screen.
  int x2 = (int) (size.width * 0.80);
  
  //Create object of TouchAction class.
  TouchAction action = new TouchAction((MobileDriver)driver);
  
  //Find element to swipe from right to left.
  WebElement ele1 =  (WebElement) driver.findElementsById("com.fortysevendeg.android.swipelistview:id/front").get(3);  
  //Create swipe action chain and perform horizontal(right to left) swipe.
  //Here swipe to point x1 Is at left side of screen. So It will swipe element from right to left.
  action.longPress(ele1).moveTo(x1,580).release().perform();
  
  //Find element to swipe from left to right.
  WebElement ele2 =  (WebElement) driver.findElementsById("com.fortysevendeg.android.swipelistview:id/back").get(3);
  //Create swipe action chain and perform horizontal(left to right) swipe.
  //Here swipe to point x2 Is at right side of screen. So It will swipe element from left to right.
  action.longPress(ele2).moveTo(x2,580).release().perform();
 }

 @AfterTest
 public void End() {
  driver.quit();
 }
}

Test Explanation
  • ele1 Is an element located to swipe from right side to left side.
  • ele2 Is an element located to swipe from left side to right side.
  • longPress() method will press and hold given element.
  • moveTo(X, Y) method will move element to given X and Y coordinates.
  • release() method will remove the current touching implement from the screen.
  • perform() will perform this chain of actions on the driver.
Now start appium server and run test script In eclipse and observe element swipe operation in your android mobile device. This way you can swipe specific element of android app using action chain.

10 comments:

  1. Hi, I have implemented same action in my script, but right swipe always seems to be making left swipe. Left swipe though works perfectly normal.
    Any suggestion where I may be going wrong.

    ReplyDelete
  2. above same error for me... anything sugguest?

    ReplyDelete
  3. same error for me. Anything suggest for me?

    ReplyDelete
  4. same error for me. anything suggest for me?

    ReplyDelete
  5. This dosent work. Appium 1.5 is now changed to use relative coordinates.

    There is bug in Appium for scrolling IOS app. please refer https://github.com/appium/appium/issues/6918

    ReplyDelete
  6. here you specified same element for left and right swipe, but in run time it tacking any two element for swipe action ???

    ReplyDelete
  7. in run time how it choosing two different element, in code you only mention single element ???

    ReplyDelete
  8. Hi,

    I can find "com.fortysevendeg.android.swipelistview:id/front"
    but can't find "com.fortysevendeg.android.swipelistview:id/back" with UI automator viewer.

    Mak

    ReplyDelete
  9. Please provide full source code so that beginner understand it.

    ReplyDelete
  10. can u provide same swipe code for IOS

    ReplyDelete