Home » appium-tutorial » Vertical And Horizontal swipe/scroll in Appium

Newly Updated Posts

Vertical And Horizontal swipe/scroll in Appium

Previous Topic, We are going to learn the Vertical And Horizontal swipe/scroll in Appium in this tutorial. So lets start.

What is Swipe/Scroll in mobile device?

Swipe/Scroll is a touch Action which is performed in mobile device to move the mobile screen from top to bottom or bottom to top , so that the user can view the hidden elements of mobile application and perform action or event accordingly.

Vertical and horizontal scrolling/swipe are the two different approaches by which user move the application with in screen.

In vertical swipe/scroll, mobile application is moved from top to bottom by touch action and vice versa, where x co ordinates remains the same but y co ordinates changes simultaneously.

Similarly in horizontal swipe/scroll, mobile application on device is moved from left to right Or right to left, where x co-ordinates changes but y co-ordinates remains the same.

Note: In this Tutorial we will be take the example of Flipkart moble application to understand in depth and inspect its locator.

How to perform Vertical And Horizontal swipe/scroll in Appium

Vertical And Horizontal swipe/scroll in Appium

1 ) Vertical Scrolling: As we discussed above what is vertical scrolling, now we will discuss, how can we perform through Appium, Below is the code for vertical scrolling in appium.

TouchAction  action =new TouchAction(driver);	
Dimension size	=driver.manage().window().getSize();
int width=size.width;
int height=size.height;				
int middleOfX=width/2;
int startYCoordinate= (int)(height*.7);
int endYCoordinate= (int)(height*.2);
				
action.press(PointOption.point(middleOfX, startYCoordinate))
.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
.moveTo(PointOption.point(middleOfX, endYCoordinate)).release().perform();

Code Description:(Vertical Scrolling)

Step 1) We have created the object of TouchAction by passing driver as a parameter.

TouchAction action =new TouchAction(adriver);

Step 2) Get the size of the mobile application window and extract its height and width and store it in a variable.

Dimension size =driver.manage().window().getSize();

int width=size.width;

int height=size.height;

Step 3) Now get the x co-ordinates of the middle of mobile application and store it in a variable by dividing the width by 2.

int middleOfX=width/2;

Step 4) Now take the start y co ordinate and end y co ordinate from where the swipe/scroll is to be

int startYCoordinate= (int)(height.7);

int endYCoordinate= (int)(height.2);

Step 5) Now press the element from starting element taking (x,y) co ordinate and wait for 2 seconds and then move to the destination element by (x,y) co ordinate.

action.press(PointOption.point(middleOfX, startYCoordinate))
.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
.moveTo(PointOption.point(middleOfX, endYCoordinate)).release().perform();

How to perform Horizontal Swipe/Scroll in Appium?

2) Horizontal Scrolling: we have dicussed the horizontal scrolling above, now lets perform horizontal scrolling in appium.

List<AndroidElement> e=adriver.findElements(MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"com.flipkart.android:id/banner_image\")"));
AndroidElement firdelement=e.get(0);
AndroidElement secondElement=e.get(1);
AndroidElement thirdElement=e.get(2);
							
int midOfY =thirdElement.getLocation().y +(thirdElement.getSize().height/2);
int fromXLocation=thirdElement.getLocation().x;
int toXLocation=firdelement.getLocation().x;
						
TouchAction  action =new TouchAction(adriver);
action.press(PointOption.point(fromXLocation, midOfY))
.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(3)))
.moveTo(PointOption.point(toXLocation, midOfY))
.release()
.perform();

Code Description:(Horizontal Scrolling)

Step 1) Firstly inspect the elements of banner image at the top (as they all have common attribute value, so take all element on list.

List e=adriver.findElements(MobileBy.AndroidUIAutomator(“new UiSelector().resourceId(\”com.flipkart.android:id/banner_image\”)”));

Step 2) Get all the 3 android elements by their index.

AndroidElement firdelement=e.get(0);
// AndroidElement secondElement=e.get(1);
AndroidElement thirdElement=e.get(2);

Step 3) Now get the y co-ordinates till the mid of the image element by adding the location of y and half the size of image,

int midOfY =thirdElement.getLocation().y +(thirdElement.getSize().height/2);

Step 4) Now get the x coordinates of the element from which the swipe to be performed and the x coordinates to which the swipe is drived.

int fromXLocation=thirdElement.getLocation().x;
int toXLocation=firdelement.getLocation().x;

Step 5) Now press the element from starting element taking (x,y) co ordinate and wait for 2 seconds and then move to the destination element by (x,y) co ordinate.

action.press(PointOption.point(fromXLocation, midOfY))
.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
.moveTo(PointOption.point(toXLocation, midOfY))
.release()
.perform();

#code2test #automationtesting #Appium
How to Perform Horizontal Swipe/Scroll in Appium | Touch Action| Android device

Code in Eclipse will look as below screenshot:

Vertical And Horizontal swipe/scroll in Appium

Conclusion:

So in this tutorial we have learned to perform vertical and horizontal scrolling in appium using the touch action with multiple commands.