Saturday 7 October 2017

Swipe Right,Left,Up & Down using Appium

This is very useful tutorial for those who are learning appium as swiping and scrolling is commonly used for almost all mobile apps. So learning to automate it using Appium is going to help you in writing test code.Below is the code where I have written different functions for swiping from top to bottom, from bottom to top, from left to right, from right to left using Appium. We can all these methods in our test cases to perform required actions. plus I have created a video tutorial with detailed explanation.



/**
  /* Method to swipe screen from Bottom to Top (Vertical) Get the size of
  * screen. Find swipe start and end point from screen's width and height.
  * Find starty point which is at bottom side of screen. Find endy point
  * which is at top side of screen. Find horizontal point where you wants to
  * swipe. It is in middle of screen width.
  * Time duration should be in milliseconds
  */

 public void bottomTopswipe(int timeduration) {

  
  size = driver.manage().window().getSize();
  System.out.println(size);
  starty = (int) (size.height * 0.50);
  endy = (int) (size.height * 0.20);
  startx = size.width / 2;
  System.out.println("Start swipe operation");
  driver.swipe(startx, starty, startx, endy, timeduration);

 }

 /**
  * 
  * Method to swipe screen from Top to Bottom (Vertical) Get the size of
  * screen. Find swipe start and end point from screen's width and height.
  * Find starty point which is at bottom side of screen. Find endy point
  * which is at top side of screen. Find horizontal point where you wants to
  * swipe. It is in middle of screen width. 
         * Time duration should be in milliseconds
   */

 public void topBottomswipe(int timeduration) {

  
  size = driver.manage().window().getSize();
  System.out.println(size);
  starty = (int) (size.height * 0.50);
  endy = (int) (size.height * 0.20);
  startx = size.width / 2;
  System.out.println("Start swipe operation");
  driver.swipe(startx, endy, startx, starty, timeduration);

 }

 /**
  * 
  * Method to swipe screen from right to left (Horizontal) duration should be
  * in milliseconds Get the size of screen. Find swipe start and end point
  * from screen's width and height. Find startx point which is at right side
  * of screen. Find endx point which is at left side of screen. Find vertical
  * point where you wants to swipe. It is in middle of screen height. 
         * Time duration should be in milliseconds
   */

 public void rightLeftSwipe(int timeduration) {

  size = driver.manage().window().getSize();
  System.out.println(size);
  startx = (int) (size.width * 0.70);
  endx = (int) (size.width * 0.30);
  starty = size.height / 2;
  System.out.println("Start swipe operation");
  driver.swipe(startx, starty, endx, starty, timeduration);

 }

 /**
  * 
  * Method to swipe screen from left to right (Horizontal) duration should be
  * in milliseconds Get the size of screen. Find swipe start and end point
  * from screen's width and height. Find startx point which is at right side
  * of screen. Find endx point which is at left side of screen. Find vertical
  * point where you wants to swipe. It is in middle of screen height. 
         * Time duration should be in milliseconds
      */

 public void leftRightSwipe(int timeduration) {
  // duration should be in milliseconds
  size = driver.manage().window().getSize();
  System.out.println(size);
  startx = (int) (size.width * 0.70);
  endx = (int) (size.width * 0.30);
  starty = size.height / 2;
  System.out.println("Start swipe operation");
  driver.swipe(endx, starty, startx, starty, timeduration);

 }

22 comments:

  1. Thank you for wonderful tutorial.
    Need help from you. I tried the above code, but it showing error at driver.swipe()

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

    Can you please let me know the resolution

    ReplyDelete
  2. How to swipe horizontally as swipe method is removed. i am able to swipe vertically but horizontally i am facing some issues.

    ReplyDelete
    Replies
    1. Yes, these codes need to be updated as per latest release. Waiting for the same.

      Delete
  3. 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
  4. This comment has been removed by the author.

    ReplyDelete
  5. Thank you on your manual. I've done and successful
    subway surfers

    ReplyDelete
  6. error:- The method Swipe(int, int, int, int, int) is undefined for the type AndroidDriver

    ReplyDelete
  7. Thanks for such a great article here. I was searching for something like this for quite a long time and at last I've found it on your blog. It was definitely interesting for me to read about their market situation nowadays.
    oracle training in chennai

    oracle training in tambaram

    oracle dba training in chennai

    oracle dba training in tambaram

    ccna training in chennai

    ccna training in tambaram

    seo training in chennai

    seo training in tambaram

    ReplyDelete
  8. size cannot be resolved to a variable

    i am getting error message for "size"

    ReplyDelete
  9. do we have any other option without creating method to scroll down for Android platform.

    ReplyDelete