1

How can we write scrolling code in Appium with Java?

Here is what I want to do,

  1. Open the application.
  2. Scroll down to the footer.
  3. Scroll back up.

This is the code that I have written:

base.java ;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;

public class base {
    
public static  AndroidDriver<AndroidElement> capabilities() throws MalformedURLException
{
    
     File f = new File("src");
     File fs = new File(f, "ApiDemos-debug.apk");
    
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(MobileCapabilityType.DEVICE_NAME,"oppo A5s");
    cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Uiautomator2");
    cap.setCapability(MobileCapabilityType.VERSION, "8.1.0");
    cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
    cap.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
    AndroidDriver<AndroidElement>driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);        
     return driver ;
    

}

Scolling class;

import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

public class ScrolingDemo extends base {

public static void main(String[] args) throws MalformedURLException {
    // TODO Auto-generated method stub
    AndroidDriver<AndroidElement>driver=capabilities();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.TextView[@text='Views']")).click();

2 Answers 2

1
   driver.findElement(MobileBy.AndroidUIAutomator(
            "new UiScrollable(new UiSelector().scrollable(true)).scrollToEnd(100000)"));
   driver.findElement(MobileBy.AndroidUIAutomator(
            "new UiScrollable(new UiSelector().scrollable(true)).scrollToBeginning(100000)"));

you can use adroidUIAutomater class's uiscrollable,

https://developer.android.com/reference/androidx/test/uiautomator/UiScrollable

1
0

Please try putting this method in the base class:

    public void scrollToWebElement(WebElement locator)
    {
        try
        {
            JavascriptExecutor js = (JavascriptExecutor) driver;    
            js.executeScript("arguments[0].scrollIntoView();", locator);
        }
        catch (Exception e)
        {
            System.err.println("Unable to scroll to webelement.  WebElement is not visible.");
        }
    }
12
  • hi, sir, which locator I have given here I did not understand. Mar 3, 2021 at 5:54
  • basically, I want to just scroll the page at the bottom and then comes up with that which locator I use . Mar 3, 2021 at 5:56
  • WebElement myLocator = driver.findElement(By.xpath("//android.widget.TextView[@text='Views']")); scrollToWebElement(myLocator);
    – ToastMan
    Mar 3, 2021 at 16:43
  • This should take you directly to your webelement. Not sure why you want to scroll all the way to the bottom.
    – ToastMan
    Mar 3, 2021 at 16:48
  • He want to scroll application not webapp
    – PDHide
    Mar 3, 2021 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.