iOS | PickerWheel automation

Learn how to automating PickerWheel UI element testing by running an Appium script over the XCUITest infrastructure.

Apple PickerWheel view APIs

Apple applies two main APIs for implementing the PickerWheel in an application UI. These APIs refer to the types of subelements organized in the PickerWheel rows. The API types are defined by the developer that developed the application:

  • Title for row: When the PickerWheel values are of type "string," arranged in rows.
  • View for row: When the PickerWheel values are of type "view," arranged in rows. This type enables the developer to design each row with a more complex UI, for example with images.

These API types are not visible when inspecting the application with the Object Spy widget. For a string value, consider using the Automation API that is suitable for the "title for row" API. If it does not work, try using the API that is suitable for "view for row."

Automation API (via Appium XCUITest)

  • Automating "title for row" PickerWheel view type using the "sendKeys" command:

    Copy
    element.sendKeys("title");
  • Automating "view for row" PickerWheel view type using the "executeScript" command:

    Copy
    Map<String, Object> params = new HashMap<>();
    params.put("order", direction);
    params.put("offset", offset);
    params.put("element", ((RemoteWebElement) pickerElement).getId());
    driver.executeScript("mobile: selectPickerWheelValue", params);

    Parameter map:

    • element (mandatory): PickerWheel's internal element id (as hexadecimal hash string) to select. The element must be of type XCUIElementTypePickerWheel.
    • order (mandatory): Either next to select the value next to the current one from the target picker wheel or previous to select the previous one.
    • offset: The value in range [0.01, 0.5]. It defines how far from picker wheel's center to perform the click. The actual distance is calculated by multiplying this value by the actual picker wheel height. Too small an offset value may not change the picker wheel value and too high a value may cause the wheel to switch two or more values at once. Usually the optimal value is located in range [0.15, 0.3]. 0.15 by default

      [Reference: Appium pickerWheel automation API]

Sample code

Copy

PickerWheel automation sample

// The pickerwheel element must be this specific type ("XCUIElementTypePickerWheel"), 
// not “XCUIElementTypePicker” or any other parent/child of the pickerwheel.
WebElement picker = driver.findElementByClassName("XCUIElementTypePickerWheel")


/*### Title based pickerwheel ##*/
driver.findElementByName("Single picker").click();
picker = driver.findElementByClassName("XCUIElementTypePickerWheel");
String name = picker.getAttribute("value");
picker.sendKeys("Title 8");
picker.sendKeys("Title 2");

/*### View based pickerwheel ##*/
pickerwheelStep(driver,picker,"next",0.15);
pickerwheelStep(driver,picker,"next",0.15);
pickerwheelStep(driver,picker,"next",0.15);
pickerwheelStep(driver,picker,"next",0.15);
pickerwheelStep(driver,picker,"previous",0.15);
pickerwheelStep(driver,picker,"previous",0.15);
pickerwheelStep(driver,picker,"previous",0.15);

private static void pickerwheelStep(IOSDriver driver, WebElement element, String direction, double offset) {
    Map<String, Object> params = new HashMap<>();
    params.put("order", direction);
    params.put("offset", offset);
    params.put("element", ((RemoteWebElement) element).getId());
    driver.executeScript("mobile: selectPickerWheelValue", params);
}

/*### Get selected value ###*/

// title based will retrieve the title as a string, 
// view based will retrieve a string that represent the view 
//(uniqueness depends on the developer of the app).
name = picker.getAttribute("value")