Automate Picker Wheel Controls and Physical Buttons on iOS Devices

iphone6_hands_resetpicker_2x

     Here I would like to share two common activities while automating in iOS devices, automation of picker wheel and actions over the physical buttons. First, we will discuss the automation of the picker wheel.

      Appium also makes available a mobile: method called selectPickerWheelValue, which is primarily useful for navigating a picker wheel using forward-and-back gestures. The command takes 3 parameters:

  • order: whether you want to go forward (“next”) or backward (“previous”) in the list of options.
  • offset: how far (in ratios of the picker wheel height) you want the click to happen. The default is 0.2.
  • element: the internal ID of the picker wheel element we’re working with.

     Once we know what we want to do, we simply have to construct our typical executeScript call with these parameters. Below is the sample code snippet to do picker wheel automation:

HashMap<String, Object> params = new HashMap<>();
params.put(“order”, “next”);
params.put(“offset”, 0.15);
params.put(“element”, ((RemoteWebElement) pickerWheelElement).getId());
driver.executeScript(“mobile: selectPickerWheelValue“, params);

Below is the sample example code to access date from the picker wheel,

private static By pickers = MobileBy.className(“XCUIElementTypePickerWheel”);

// find the picker elements
List<WebElement> pickerEls = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(pickers));

// use the selectPickerWheelValue method to move to the next value in the ‘month’ wheel
HashMap<String, Object> params = new HashMap<>();
params.put(“order“, “next“);
params.put(“offset“, 0.15);
params.put(“element“, ((RemoteWebElement) pickerEls.get(0)).getId());
driver.executeScript(“mobile: selectPickerWheelValue“, params);

// and move to the previous value in the ‘day’ wheel
params.put(“order“, “previous“);
params.put(“element“, ((RemoteWebElement) pickerEls.get(1)).getId());
driver.executeScript(“mobile: selectPickerWheelValue“, params);

       Sometimes there’s an automation feature of apparently little utility, but it’s fun all the same. For some reason, Apple decided to allow the automation of the physical buttons on an iPhone. Some of them are the home button, the volume up, and down buttons. They’re accessible as one of Appium’s mobile: methods, like many other non-standard extensions to the WebDriver protocol. Here’s an example of how to press the home button:

driver.executeScript(“mobile: pressButton“, ImmutableMap.of(“name”, “home“));

In other words, there’s a single parameter name, and it takes one of three values:

  • home
  • volumeup
  • volumedown

Below is the example of how to press the volume up and volume down buttons:

driver.executeScript(“mobile: pressButton“, ImmutableMap.of(“name”, “volumeup“));

driver.executeScript(“mobile: pressButton“,ImmutableMap.of(“name”, “volumedown“));

     The only catch is that the volumeup and volumedown buttons can only be automated on a real device, not a simulator. Pressing the home button is a good way to get out of your app and back to the home screen. Pressing it twice will navigate back to the first home screen. And pressing it twice in quick succession should open up the app switcher.

   Please try to use the above picker wheel and physical button actions in your automation during the iOS application automation. Please use the latest java-client and Appium server.

Reference: Appium Pro

make it perfect!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s