SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

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 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.

1. Horizontal Swiping In Android App
Using appium, 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 bottom to top and top to bottom vertically as shown in bellow Image.


This is our goal to achieve from this post.

Create And Run Appium Android Test Script To 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.

driverSwipe.java
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 vertical swipe 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.

7 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