0

I have a list of items with checkboxes 1,2...n

I used this code to get the list of the current view and select the items. How could I scroll the list to select the next items in the list and repeat this process until the end ?

List<WebElement> elements = driver.findElements(By.xpath("//android.widget.CheckBox"));
        for(WebElement element: elements){
            element.click();
        }
6
  • I know you've posted this elsewhere - what have you tried? Do you have working code to scroll and just need to understand how to tell when you've reached the end of the list? How to implement via page object? What? Aug 9, 2018 at 16:54
  • I need a way where I can scroll until I have a page view with the next items to select. It is also fine if I learn just how to scroll to an n element in the list. Aug 9, 2018 at 17:01
  • I have two different routines that scroll, both are untested, but I've posted them to various similar questions. I've received minimal feedback on their success but they do appear to work for most people. Would you prefer an example using TouchActions or javascript? Beyond that, various methods in java would be used to determine when you've reached the end of a list or when you've found a match. Aug 9, 2018 at 17:30
  • Somehow Touch Actions didn't seem to work but no issues let me try your solution with Touch Actions Aug 9, 2018 at 17:31
  • Are you running your tests on a device or on emulator?
    – Alexey R.
    Aug 11, 2018 at 7:10

2 Answers 2

1

Note that I use the Selenium import, not the TouchAction of Appium:

import org.openqa.selenium.interactions.touch.TouchActions;

The scrolldown method using TouchActions:

public void scrollDown() throws Exception {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting y location set to 80% of the height (near bottom)
    int starty = (int) (size.height * 0.80);
    //Ending y location set to 20% of the height (near top)
    int endy = (int) (size.height * 0.20);
    //x position set to mid-screen horizontally
    int startx = size.width / 2;

    new TouchActions(driver)
            .down(startx, starty)
            .move(startx, endy)
            .release()
            .build()
            .perform();

}

Please let me know if this works for you. If it does not, I will post a separate answer with the Javascript Executor.

6
  • Note, it's assumed that the class that contains this method has a copy of the webdriver as "driver" Aug 9, 2018 at 17:37
  • I will try this and let you know if I succeed Aug 9, 2018 at 17:54
  • 1
    I get the below error - java.lang.ClassCastException: io.appium.java_client.android.AndroidDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen at org.openqa.selenium.interactions.touch.TouchActions.<init>(TouchActions.java:38) at NewGroupCreation.GroupCreation(NewGroupCreation.java:128) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) And more .... Aug 10, 2018 at 12:51
  • I'm not sure what would be causing that particular casting exception. Try the javascript executor version I posted in a second answer. Aug 10, 2018 at 13:00
  • I'm currently working on a script to implement the above routine (as well as test the other option) but it's not yet in a state to be tested. It could well be that there's a flaw in the logic, which was why I stated it was untested, but the problem might still lie elsewhere (versioning, webdriver, etc.) Aug 10, 2018 at 13:02
0

Here is the version that uses the javascript executor:

public void jsScrollDown() throws Exception {

    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap<String, String> scrollObject = new HashMap<String, String>();
    scrollObject.put("direction", "down");
    js.executeScript("mobile: scroll", scrollObject);

}
2
  • I get this error, org.openqa.selenium.UnsupportedCommandException: Unknown mobile command "scroll". Only shell,startLogsBroadcast,stopLogsBroadcast commands are supported. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at and more ...... Aug 10, 2018 at 13:03
  • Looks like I need to finish my test script project. Sorry I wasn't able to help. Aug 10, 2018 at 13:27

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.