Thursday, August 29, 2019

How to Swipe down or Scroll Down in Appium with example | Appium Mobile Automation


How to Swipe down in Appium  with example | Appium Mobile Automation



Lets learn how to swipe down or scroll down in android mobile, In most of the time we use this action to scroll from top to bottom page elements. Some times we will not find the elements on the screen, before clicking or doing any action first we need to make sure that element is visible.


Example:

I’m taking example of Api Demos app and swiping down from top to down.











App used: Api Demos 

Steps:
1. Open Api Demos app 





2. First we need find the xpath for “Views” menu :








Xpath for views menu is :  //*[@text='Views']

 3. Click on “Views”

 4. In the views page perform swipe down action

Before swipe down screen looks like this :




5. First find the element locator from where you want to swipe down:





in the above screenshot, i'm taking "Gallery" xpath.

6. Find the element location - X axis and Y axis & provide how many pixels you want swipe down:

Point value = null;
   value = driver.findElement(By.xpath("//*[@text='Gallery']")).getLocation();

    int x = value.x;
   int y = value.y;
   int y1 = value.y+pixelsToSwipe;



7. Then by passing x axis and Y axis you can call the swipe() method:

swipe(x,y1,x,y);

8. Swipe method code:



public static void swipe(int fromX,int fromY,int toX,int toY) {
 
 TouchAction action = new TouchAction(driver);
 action.press(PointOption.point(fromX,fromY))
 .waitAction(new WaitOptions().withDuration(Duration.ofMillis(3000))) //you can change wait durations as per your requirement
 .moveTo(PointOption.point(toX, toY))
 .release()
 .perform();
 }



9. copy the below complete code for swipe down action and paste in you class file:



package practise;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;

public class SwipeToDown {
 
 static AndroidDriver<MobileElement> driver;
 
 @Test
  public void setup() throws InterruptedException, MalformedURLException {
 
     DesiredCapabilities cap=new DesiredCapabilities();
     cap.setCapability("deviceName", "emulator-5554");
     cap.setCapability("udid", "emulator-5554");
     cap.setCapability("appPackage", "io.appium.android.apis");
     cap.setCapability("appActivity", "io.appium.android.apis.ApiDemos");
     cap.setCapability("platformName", "Android");
     cap.setCapability("platformVersion", "9.0");
     driver=new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), cap);
     driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
     Thread.sleep(5000);
     
        driver.findElementByXPath("//*[@text='Views']").click();
        Thread.sleep(5000);
        swipeDown(250);
     
}
 
 public void swipeDown(int pixelsToSwipe) {

  try {
 Point value = null;
 value = driver.findElement(By.xpath("//*[@text='Gallery']")).getLocation();
 int x = value.x;
 int y = value.y;
 int y1 = value.y+pixelsToSwipe;

  swipe(x,y1,x,y);
 
 } catch(Exception e) {
 System.out.println(e.getMessage());
 }

  }
 
 public static void swipe(int fromX,int fromY,int toX,int toY) {
 
 TouchAction action = new TouchAction(driver);
 action.press(PointOption.point(fromX,fromY))
 .waitAction(new WaitOptions().withDuration(Duration.ofMillis(3000))) //you can change wait durations as per your requirement
 .moveTo(PointOption.point(toX, toY))
 .release()
 .perform();
 }
 

}

10. Run the above as TestNg Test:

After swipe down : execution result of the screen



Console Output:




TestNg Output:


No comments:

Post a Comment

If any suggestions or issue, please provide