How to perform scroll/swipe top to bottom

Hi,

I am working on mobile App testing. I need help from you guys.
I want to know, how to scroll/swipe from too to bottom in Appium.
Also need to know, how to scroll until particular element found?
Please check the below details which i am using.

Appium version @1.7
Android version @6.0.1
Device: Real Device(Redmi)
TestNG

1 Like

you can use while loop contains swipe function and set in your condition to check if element appear

Ex :

While (driver.findElements(By.id(“your_id”)).size()==0){

size = driver.manage().window().getSize();
int starty = (int) (size.height * 0.80);
int endy = (int) (size.height * 0.20);
int startx = size.width / 2;
System.out.println("starty = " + starty + " ,endy = " + endy + " , startx = " + startx);

driver.swipe(startx, starty, startx, endy, 3000);
Thread.sleep(2000);
driver.swipe(startx, endy, startx, starty, 3000);
Thread.sleep(2000);
}

once your element appear while will be stop and you can execute your action on it :slight_smile:

i hope this help you

@Bilal_Jarwan swipe function already deprecated from the 1.6.5 version onwards.so this function is not going to work.For swiping you have to use the TouchAction class.

@suryakanta939 its working for me, that’s why i suggest this solution

@Bilal_Jarwan yes it will work but for the appium version bellow 1.6.5 .But question was for appium 1.7
Thanks:slightly_smiling_face:

@suryakanta939

yes, that’s right :slight_smile: thanks, also i have set the solution for the main issue :wink: how to swipe until you find you element.

Heya, since I am struggling with the scrolling on Appium 1.7, can you please give me the example of the TouchAction class, or at least point me to the thread someone already explained?

Refer to Swipe/Scroll best practice with Java-Client 5 for consolidated answers

Not working for me, but I managed to make it work with TouchAction, see bellow.

TouchAction ta = new TouchAction(driver);
ta.press(Music).moveTo(GC).release().perform();

Basically UiScrollable and UiSelector are the classes of android.
You just need to enter element’s text to scroll up to that element.

driver.findElementByAndroidUIAutomator(“new UiScrollable(new UiSelector()).scrollIntoView(text(“Enter your element”))”);

2 Likes

How would you perform scroll if you have no other way to catch element than using webview?
So you switchdriver webview and you want then to grab element and scroll, is it possible by UiAutomator?

Here is an example using (x,y) coordinates

TouchAction ts = new TouchAction(driver);
ts.press(207, 582).moveTo(8, -360).release().perform();

1 Like

Working, but how you come across those numbers(coordinates)?
Is there any more sophisticated way of scrolling when dealing with webview?

1 Like

@Bilal_Jarwan
I tried to use the below code…
Dimension size = driver.manage().window().getSize();
int starty=(int)(size.height0.5);
int endy=(int)(size.height
0.2);
int startx=size.width/2;
driver.swipe(startx,endy,startx,starty,2000);

But getting an error message like: “The method swipe(int, int, int, int, int) is undefined for the type AndroidDriver”. Can you please let me know the resolution

@Bilal_Jarwan
I tried to use the below code…
Dimension size = driver.manage().window().getSize();
int starty=(int)(size.height0.5);
int endy=(int)(size.height
0.2);
int startx=size.width/2;
driver.swipe(startx,endy,startx,starty,2000);

But getting an error message like: “The method swipe(int, int, int, int, int) is undefined for the type AndroidDriver”. Can you please let me know the resolution

Can you use the inspector tool to get your x/y coordinates?

Hey you can try this one ,works for me for an app devloped in react native.
I hope the comments will help you .
public void scrollUp() throws Exception {
// Get the size of screen.
Dimension size = driver.manage().window().getSize();
// Find swipe start and end point from screen’s with and height.
// Find start y point which is at bottom side of screen.
int starty = (int) (size.height * 0.80);
// Find end y 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;

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

Sorry, but i have not tried for web view but you can use this single line code instead of defining a function and using it .i think there wont be an element without a text defining it, for what that element is .

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<String, String> scrollObject = new HashMap<String, String>();
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<String, String> scrollObject = new HashMap<String, String>();
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<String, String> scrollObject = new HashMap<String, String>();
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;
}

public void swiptToBottom()
	{
		Dimension dim = driver.manage().window().getSize();
		int height = dim.getHeight();
		int width = dim.getWidth();
		int x = width/2;
		int top_y = (int)(height*0.80);
		int bottom_y = (int)(height*0.20);
		System.out.println("coordinates :" + x + "  "+ top_y + " "+ bottom_y);
		TouchAction ts = new TouchAction(driver);
		ts.press(x, top_y).moveTo(x, bottom_y).release().perform();
	}

Call the above method to swipe to bottom

1 Like