Versions used while doing this blog post,
Appium server version : 1.16.0
Appium JAVA client version : 7.3.0
In the latest version of Appium JAVA client, we don’t have direct method to perform scroll functionality. In this post, lets frame a logic to perform vertical scroll within android app. This logic can be tweaked to perform horizontal scroll as well.
Steps:
1. Instantiate (creation of object) TouchAction class which can be imported from io.appium.java_client package
TouchAction action = new TouchAction(your_driver_object);
2. Get the height and width of the app/device screen [Dimension class can be imported from org.openqa.selenium package]Dimension screenSize = driver.manage().window().getSize();
3. To perform vertical scroll, we need an ‘x coordinate based on width’ and ‘start y & end y coordinates based on height’. Lets find it:int widthX = screenSize.width / 2; //x coordinate from middle area of screen width
int startPointY = (int) (screenSize.height * 0.80); // start y coordinate from lower area of screen height
int endPointY = (int) (screenSize.height * 0.20); // end y coordinate from upper area of screen height
4. Scroll can be performed using the combination of ‘press’, ‘waitAction’, ‘moveTo’ & ‘release’ methods of TouchAction classaction.press(PointOption.point(widthX, startPointY)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2))).moveTo(PointOption.point(widthX, endPointY)).release().perform();
Full code for reference: (Note : The APK file of android app was in project path for below example)
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
public class ScrollFunctionality {
public static void main(String[] args) throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "****"
);
capabilities.setCapability(MobileCapabilityType.APP, System.getProperty("user.dir")+"\\src\\main\\resources\\ApiDemos-debug.apk");
URL url = new URL("http://0.0.0.0:4723/wd/hub");
AndroidDriver driver = new AndroidDriver(url, capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(driver.findElementByAccessibilityId("Views")));
driver.findElementByAccessibilityId("Views").click();
wait.until(ExpectedConditions.visibilityOf(driver.findElementByAccessibilityId("Chronometer")));
TouchAction action = new TouchAction(driver);
Dimension screenSize = driver.manage().window().getSize();
int widthX = screenSize.width / 2;
int startPointY = (int) (screenSize.height * 0.80);
int endPointY = (int) (screenSize.height * 0.20);
action.press(PointOption.point(widthX, startPointY)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2))).moveTo(PointOption.point(widthX, endPointY)).release().perform();
wait.until(ExpectedConditions.visibilityOf(driver.findElementByAccessibilityId("ImageButton")));
driver.findElementByAccessibilityId("ImageButton").click();
driver.closeApp();
driver.quit();
}
}
Wow! Logic for vertical scroll working fine.. See the output video here..