Appium - How To Swipe Vertical And Horizontal In Android Automation

Earlier In previous post, we learnt how to interact with android mobile gesture to perform drag and drop by generating action chain using TouchAction class of Webdriver 3. Swiping is another common action for any android mobile app. As you know, We can swipe horizontally(left to right or right to left) and swipe vertically(bottom to top and top to bottom) In android mobile app. Here I have described how to swipe horizontally and vertically In android mobile app using driver.swipe() when running swipe in appium automation test. Follow me throughout this article to learn how to swipe in android app using appium.

PREREQUISITES : Previous 19 steps(PART 1 and PART 2) of appium tutorials should be completed.

Download And Install SwipeListView Demo App
We will use SwipeListView Demo App for android swipe test using appium. You need to download and install SwipeListView Demo App in your android mobile device.
  • You can Download it from Google Play Store or THIS PAGE.
  • Install it in your android mobile device.
  • Open SwipeListView Demo App In your mobile device. It will show you alert message on screen.
  • Select "Don't show this message again" check box and click on OK button as shown in bellow image. Now this message will not display again when we run our test through appium.

  • It will show you list of apps in your android device. Using this app, You can swipe horizontal and vertical.
Aim To Achieve In This Appium Test
I have a 2 goals to achieve from this post in appium. 1. Horizontal swiping and 2. Vertical swiping in android app. Also you can do appium swipe down or appium swipe up in same way.

1. Horizontal Swipe In Appium for Android App

Using appium swipe, We wants to swipe right to left and left to right horizontally as shown in bellow Image.


2. Vertical Swiping In Android App
Also we wants to swipe down in appium and swipe up in appium as shown in bellow Image.


This is our goal to achieve from this post.

Create And Run Appium Android Test Script for appium Swipe

I have prepared simple test to perform swipe on SwipeListView Demo android app as shown bellow. Create new class file driverSwipe.java and paste bellow given test script code in it.

swipe appium java example

package Android;

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.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 driverSwipe {
 AndroidDriver driver;
 Dimension size;
 @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);
  WebDriverWait 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 start and end point from screen's with and height.
  //Find startx point which is at right side of screen.
  int startx = (int) (size.width * 0.70);
  //Find endx point which is at left side of screen.
  int endx = (int) (size.width * 0.30);
  //Find vertical point where you wants to swipe. It is in middle of screen height.
  int starty = size.height / 2;
  System.out.println("startx = " + startx + " ,endx = " + endx + " , starty = " + starty);

  //Swipe from Right to Left.
  driver.swipe(startx, starty, endx, starty, 3000);
  Thread.sleep(2000);

  //Swipe from Left to Right.
  driver.swipe(endx, starty, startx, starty, 3000);
  Thread.sleep(2000);
 }

 @Test
 public void swipingVertical() throws InterruptedException {
  //Get the size of screen.
  size = driver.manage().window().getSize();
  System.out.println(size);
   
  //Find swipe start and end point from screen's with and height.
  //Find starty point which is at bottom side of screen.
  int starty = (int) (size.height * 0.80);
  //Find endy point which is at top side of screen.
  int endy = (int) (size.height * 0.20);
  //Find horizontal point where you wants to swipe. It is in middle of screen width.
  int startx = size.width / 2;
  System.out.println("starty = " + starty + " ,endy = " + endy + " , startx = " + startx);

  //Swipe from Bottom to Top.
  driver.swipe(startx, starty, startx, endy, 3000);
  Thread.sleep(2000);
  //Swipe from Top to Bottom.
  driver.swipe(startx, endy, startx, starty, 3000);
  Thread.sleep(2000);
 }

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

}

swipingHorizontal() Method Description
In above test script, swipingHorizontal() method is responsible for horizontal swipe. Here,
  • driver.manage().window().getSize(); will find your device's screen size(Width X Height).
  • startx Is located at 70% (From left) of your device's screen width.
  • endx Is located at 30%  (From left) of your device's screen width.
  • starty Is located at the vertical middle of the screen.
  • First driver.swipe method will swipe from right side to left side as swipe start point(startx) is located at right side of the screen and end point(endx) is locate at left side of the screen. Here 3000 Is time in milliseconds to perform swipe operation. 
  • Second driver.swipe method will swipe from left side to right side as swipe start point(endx) is located at left side of the screen and end point(startx) is locate at right side of the screen.
  • Vertical point starty will remain steady as we are performing horizontal swipe.
swipingVertical() Method Description
swipingHorizontal() method is responsible for appium swipe up and appium swipe down In above android automation script . Here,
  • starty Is located at 80% (From top) of your device's screen height.
  • endy Is located at 20%  (From top) of your device's screen height.
  • startx Is located at the horizontal middle of the screen.
  • First driver.swipe method will swipe from bottom to top as swipe start point(starty) is located at bottom side of the screen and end point(endy) is locate at top side of the screen. Here 3000 Is time in milliseconds to perform swipe operation. 
  • Second driver.swipe method will swipe from top side to bottom as swipe start point(endy) is located at top side of the screen and end point(starty) is locate at bottom side of the screen.
  • Horizontal point startx will remain steady as we are performing horizontal swipe.
I hope, Now you aware about how to run appium test script In android mobile device. Start appium server and run test script in eclipse and observe swipe operation in your android mobile screen.

This way you can swipe horizontal or vertical in any android application.

17 comments:

  1. I have to relocate the webelement from bottom to top, but above method is not working

    ReplyDelete
  2. driver.swipe() is showing below error

    The method swipe(int, int, int, int, int) is undefined for the type WebDriver

    ReplyDelete
  3. use AppiumDriver instead . goodLuck

    ReplyDelete
  4. Use AppiumDriver instead of Webdriver

    ReplyDelete
  5. Hay, this document help me Ton... but
    //Swipe from Bottom to Top. and //Swipe from Top to Bottom. is not working for me.
    AM using android WEBVIEW, am changing the context to NATIVE_APP to do swipe then also am not getting for TOP to BTTOM and BOTTOM to TOP.
    can you help me here....

    ReplyDelete
  6. Geeting this error .Exception in thread "main" java.lang.NullPointerException
    at aptest.aptest.swipingHorizontal(aptest.java:143)
    at aptest.aptest.main(aptest.java:234)

    ReplyDelete
  7. Hi. Is there any alternative method available which can do Swipe independent of the duration ?

    ReplyDelete
  8. HI, Aravind
    I have learned lot from your tutorials. Thank very much.

    Hi, every body here
    If you can not run the script above, you should compare it with that in previous. you have to add the miss parts in it, then it works.

    Mak

    ReplyDelete
  9. This is Out Dated Code Please provide new once......

    ReplyDelete
  10. hello @Aravind if you could provide the updated code for swipe both fro the horizontal and vertical by using appium 1.6.5 ,then it will be great

    ReplyDelete
    Replies
    1. is swipe issue clear? if cleared update code pls

      Delete
  11. I have tried this code using all below option but still getting error The method swipe(int, int, int, int, int) is undefined for the type AppiumDriver or AndroidDriver or WebDriver.

    //static WebDriver driver;
    static AndroidDriver driver;
    static AppiumDriver driver1;

    ReplyDelete
  12. Very useful post. Highly appreciate it. I just used the code for vertical scroll and it works absolutely fine. Thanks a ton :)

    ReplyDelete
  13. Hi, i have used the below code to Swipe / Scroll and it is working perfectly.
    Code to Swipe UP
    public boolean swipeFromUpToBottom()
    {
    try {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap scrollObject = new HashMap();
    scrollObject.put("direction", "up");
    js.executeScript("mobile: scroll", scrollObject);
    System.out.println("Swipe up was Successfully done.");
    }
    catch (Exception e)
    {
    System.out.println("swipe up was not successfull");
    }
    return false;
    }
    Code to Swipe DOWN
    public boolean swipeFromBottomToUp()
    {
    try {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap scrollObject = new HashMap();
    scrollObject.put("direction", "down");
    js.executeScript("mobile: scroll", scrollObject);
    System.out.println("Swipe down was Successfully done");
    }
    catch (Exception e)
    {
    System.out.println("swipe down was not successfull");
    }
    return false;
    }
    Code for carousel images swipe

    public boolean swipeImages()
    {
    try {
    WebElement pageIndicator = driver.findElement(page_indicator);
    String pageString= pageIndicator.getAttribute("value");
    int length = pageString.length();
    String count_string= pageString.substring(length-2, length).trim();
    int count = Integer.parseInt(count_string);
    System.out.println("Number of Image available to Swipe: "+count);
    for (int i=0; i<=count; i++){
    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap scrollObject = new HashMap();
    scrollObject.put("direction", "right");
    js.executeScript("mobile: scroll", scrollObject);
    }
    System.out.println("Swipe Successfully");
    }
    catch (Exception e)
    {
    System.out.println("Image swipe was not successfull");
    }
    return false;
    }

    ReplyDelete
  14. All our tests are written in Codeception for our web application which we are planning to run on iPad Safari. Is there a swipe command supported in Codeception?
    There is a command to support jQuery (https://codeception.com/docs/modules/WebDriver#executeJS), has anybody used it successfully for a swipe on a mobile device? I have been trying the below but no luck:

    $this->getModule('WebDriver')->executeJS("$('#media-list').trigger('swipe', {startX: 0.95})");

    ReplyDelete
  15. When I am initializing a driver as AndroidDriver.I am getting No such method error.
    Can you please help me by providing solution

    ReplyDelete