Last updated: Dec 21, 2017 11:10
By Guy Heimann
Supported from V10.13
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.
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):
- 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:
- 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.
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.
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.
2. Scrolling in units of the visible parts of the elements on the device screen. There are two flavors to this option:
- 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”.
- Scroll in Screen
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”.