Swipe functionality in Android

Hi,

I am trying to swipe up the screen and select the last element available in the screen. For the same, here is my code snippet:

public class Exercise {

@Test
public void test() throws MalformedURLException {
	DesiredCapabilities cap= new DesiredCapabilities();
	cap.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
	cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Device");
	cap.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.app.package");
	cap.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "com.app.activity");
	AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),cap);
	driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
	MobileElement songselection = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"songs)\")");
	songselection.swipe(SwipeElementDirection.UP, 2000);	
}

}

This script swipes the screen only to half. When I tried increased the value in the swipe function to 15000, screen scrolled up to the same half but with a varying speed.

Could you someone share your thoughts on this. Are there any new methods similar to ScrollTo available now.

Hi,

This issue got resolved by adding multiple Swipe() functions in the script. Is the right approach?

Thanks.

You can add a check using while loop. While you are on the top of the page. It will keep swiping until you reach there.

while( driver.findElement(By.id(“ID-FOR-TOP-ELEMENT”)) ) {
songselection.swipe(SwipeElementDirection.UP, 2000);
}

1 Like

Hi,

I am running into a edge case scenario where the top element is no longer visible in the screen after a swipe and the script fails. I am trying to swipe and select the last element in the screen. Please share your thoughts on the same.

Thanks.