PickerWheel automation

Automating a PickerWheel UI element testing is now very easy when 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 the sub elements organized in the PickerWheel rows.

The APIs 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 view 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 to "title for row" API. In case it does not work, try to use the API that is suitable to "view for row".

Automation API (via Appium XCUITest)

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

    Copy
    element.sendKeys("title");
  • Automating "view for row" PickerWheel view type - Use 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);

    Parameters Map:

    • element: PickerWheel's internal element id (as hexadecimal hash string) to select. The element must be of type XCUIElementTypePickerWheel. Mandatory parameter
    • order: Either next to select the value next to the current one from the target picker wheel or previous to select the previous one. Mandatory parameter
    • 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]

Demo

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")