Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Dec 21, 2017 11:10

By Guy Heimann

 

Supported from V10.13

Introduction

When creating an automation script in Appium for an iOS device that runs the XCUITest Framework, scrolling over the displayed elements may be performed with a series of TouchActions. This, however, requires that the automation script compute the proper screen coordinates for the start and end of the swipe action and adjust these for the screen resolution.

This may often result in not displaying the correct element, because the swipe action scrolled more than necessary, or not enough.

The following section presents alternatives to perform the scroll action in a more controlled automation action, and guarantee the visibility of the target UI element.

Best practice

The recommended best practice for performing controlled scrolling is using the Appium “mobile:scroll” script command. This command is performed using the executeScript() method.

This method can be executed in different ways (differentiated by the parameters supplied):

  1. Scrolling to the specific UI Element. This requires some identifier of the element that we want to make visible. Use one of the following options, based on what identifier is available:

    1. Identify the element by ‘predicateString’. In this option, the targeted UI element is a child of a scrollable container (a table or a list). This option requires two parameters:
    • element”: The id of the parent UI element – “element” must a scrollable element.
    • predicateString”: A predicate string that describes, using attribute values, the target UI element.

      For example:

      RemoteWebElement parent = (RemoteWebElement)driver.findElement(By.className("XCUIElementTypeTable")
      
      ); //identifying the parent Table
      String parentID = parent.getId();
      HashMap<String, String> scrollObject = new HashMap<String, String>();
      scrollObject.put("element", parentID);
      
      // Use the predicate that provides the value of the label attribute
      
      scrollObject.put("predicateString", "label == 'elementLabel'");
      driver.executeScript("mobile:scroll", scrollObject);  // scroll to the target element

b. If the name of the target UI element is known, you can use the name directly as a parameter to scroll to the correct position of the screen so the target element is visible.

For example:

RemoteWebElement parent = (RemoteWebElement)driver.findElement(By.className("XCUIElementTypeTable"));
String parentID = parent.getId();
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("element", parentID);
scrollObject.put("name", "elementName");
driver.executeScript("mobile:scroll", scrollObject);

c. If the id of the target element is known, then you can use the mobile:scroll script  to Scroll using the element by id.

Use the following two parameters:

      • element”: The id of the  target element you want to scroll to – The parent element of “element” must be scrollable.
      • toVisible”: The value doesn’t matter.

For example:

RemoteWebElement element = (RemoteWebElement)driver. findElement(By.name("elementName"));
String elementID = element.getId();
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("element", elementID);
scrollObject.put("toVisible", "not an empty string");
driver.executeScript("mobile:scroll", scrollObject);
Note: If the scrolling is not performing properly in any of these options, try setting the “simpleIsVisibleCheck” capability in your script to true (false by default).

              

2. Scrolling in units of the visible parts of the elements on the device screen. There are two flavors to this option:

    1. Scroll in Screen
      In this option, mobile:scroll scrolls a full screen unit in the direction provided in the single parameter, “direction”. Value of the parameter can be “up”, “down”, “left”, “right”.

 

b. Scroll in element
In this option, mobile:scroll uses the visible part of the given element as the unit of  the scrolling. If the direction is up or down – the display will be scrolled by the height of the element. If the direction is right or left the element will be scrolled by its visible width.

This option requires 2 parameters:

    • element”: The id of the element that you want to scroll – “element” must be scrollable.
    • direction”: “up”, “down”, “left, “right”.

For example:

RemoteWebElement element = (RemoteWebElement)driver.findElement(By.className("XCUIElementTypeTable"));
String elementID = element.getId();
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("element", elementID); // Only for ‘scroll in element’
scrollObject.put("direction", "down");
driver.executeScript("mobile:scroll", scrollObject);