In this post, I will show you how to do scrolling in Appium. Appium provides the
TouchAction API for gesture implementation.
In the above scrolling down example, we are trying to scroll down until the
element is visible on the screen. Here, we are getting the dimension of the
screen window; as we are scrolling so, we use windows height to manipulate the
position to scroll.
4. Scrolling Up:
1. Create an Appium driver:
First, let's create an Appium driver. The general configuration looks like as
below:
import io.appium.java_client.android.AndroidDriver;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.net.MalformedURLException;
import java.net.URL;
public class AppiumTest {
private static AndroidDriver driver;
private static WebDriverWait wait;
@Before
public static void setUp() throws MalformedURLException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("platformName", "Android");
cap.setCapability("deviceName", "your device name");
cap.setCapability("appPackage", "appPackage");
cap.setCapability("appActivity", "appActivity");
cap.setCapability("automationName", "UiAutomator1");
cap.setCapability("autoGrantPermissions", true);
cap.setCapability("autoAcceptAlerts", "true");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
wait = new WebDriverWait(driver,60);
}
@After
public static void tearDown(){
driver.quit();
}
}
This is a sample config to create an Appium driver which we will use later.
2. Scrolling:
//AppiumTest.java
private static void scroll(int scrollStart, int scrollEnd) {
new TouchAction(driver)
.press(point(0, scrollStart))
.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(10)))
.moveTo(point(0, scrollEnd))
.release().perform();
}
Here, we are dealing with scrolling, so we are adjusting the value for the
y-axis. The press() method allows you to press on the position x, y coordinates.
You can use some offset value for x coordinate instead of 0 x-offset value. The
waitAction() method will wait until the duration provided. The moveTo() method
allows moving current touch action to a new position specified. The release()
method removes the touch. Next, we will adjust the scrollStart and scrollEnd
arguments to move down and up.
3. Scrolling Down:
//AppiumTest.java
public static void scrollDown() {
MobileElement element = (MobileElement) driver.findElement(By.id("resourceId"));
if (element == null){
return;
}
int numberOfTimes = 10;
Dimension dimension = driver.manage().window().getSize();
int windowsHeight = dimension.getHeight();
int scrollStart = (int) (windowsHeight * 0.5);
int scrollEnd = (int) (windowsHeight * 0.3);
int elementLocationOffset = windowsHeight-500;
for (int i = 0; i < numberOfTimes; i++) {
int elementLocationY = element.getLocation().y;
if (elementLocationY < elementLocationOffset){
i = numberOfTimes;
System.out.println("Element available.");
}else {
scroll(scrollStart, scrollEnd);
System.out.println("Element not available. Scrolling...");
}
}
}
For e.g, if windows height is 1000 scrollStart will be 500 and scrollEnd will
be 300 which means while calling scroll() method it will press to the position
(x,y) (0, 500) and move to (x,y)(0, 300) which results in scrolling downward.
We are looping so that it will scroll down until the element will arrive to
the elementLocationOffset position where the element will be visible on the
screen.
For testing element availability, if the above approach doesn't work you can
try finding the element as below:
for (int i = 0; i < numberOfTimes; i++) {
List elements = driver.findElements(By.id("resourceId"));
if (elements.size() > 0){
i = numberOfTimes;
System.out.println("Element available.");
}else {
scroll(scrollStart, scrollEnd);
System.out.println("Element not available. Scrolling...");
}
}
If you want to scroll Up then you need to adjust the coordinates as below:
int scrollStart = (int) (windowsHeight * 0.3);
int scrollEnd = (int) (windowsHeight * 0.7);
For e.g: if the window's height is 1000 then scrollStart will be 300 and
scrollEnd will be 700 which means while calling scroll() method it will press to
the position (x,y) (0, 300) and move to (x,y)(0, 700) which results in scrolling
upward. Adjust the value that suits you.
The overall Implementation looks like below:
package appium;
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 org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import static io.appium.java_client.touch.offset.PointOption.point;
public class AppiumTest {
private static AndroidDriver driver;
@Before
public static void setUp() throws MalformedURLException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("platformName", "Android");
cap.setCapability("deviceName", "your device");
cap.setCapability("appPackage", "appPackage");
cap.setCapability("appActivity", "appActivity");
cap.setCapability("automationName", "UiAutomator1");
cap.setCapability("autoGrantPermissions", true);
cap.setCapability("autoAcceptAlerts", "true");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
}
public static void scrollDown() {
MobileElement element = (MobileElement) driver.findElement(By.id("resourceId"));
if (element == null){
return;
}
int numberOfTimes = 10;
Dimension dimension = driver.manage().window().getSize();
int windowsHeight = dimension.getHeight();
int scrollStart = (int) (windowsHeight * 0.5);
int scrollEnd = (int) (windowsHeight * 0.3);
int elementLocationOffset = windowsHeight-500;
for (int i = 0; i < numberOfTimes; i++) {
int elementLocationY = element.getLocation().y;
if (elementLocationY < elementLocationOffset){
i = numberOfTimes;
System.out.println("Element available.");
}else {
scroll(scrollStart, scrollEnd);
System.out.println("Element not available. Scrolling...");
}
}
}
private static void scroll(int scrollStart, int scrollEnd) {
new TouchAction(driver)
.press(point(0, scrollStart))
.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
.moveTo(point(0, scrollEnd))
.release().perform();
}
@After
public static void tearDown(){
driver.quit();
}
}