Search code examples
iosrubycucumbercalabashcalabash-ios

calabash-ios UIPickerView scroll to specific value


I have a UIPickerView on an iOS app. Trying to use calabash-ios to scroll down on the UIPickerView to a specific value. It is a list of years.

I tried this to see if it will scroll at least:

    Then I scroll down on "myPickerAccessibilityLabel"

Didn't work

Is there a custom step for UIPickerView's?


Solution

  • Lasse's answer is correct (I up-voted). But just to be clear, it will only work on UIDatePickers. I was just looking at the Calabash iOS sources and it looks like we have marked some of the picker methods private. The following was grabbed from the briar gem. It uses private UIKit selectors to manipulate the picker wheels. The caveat is that it might not work on non-circular (infinite) pickers.

    You can use the code below to write a Step that scrolls a picker column to row with text.

    def picker_current_index_for_column (column)
      arr = query('pickerTableView', :selectionBarRow)
      arr[column]
    end
    # methods common to generic and date pickers
    def picker_current_index_for_column_is(column, val)
      picker_current_index_for_column(column) == val
    end
    
    def previous_index_for_column (column)
      picker_current_index_for_column(column) - 1
    end
    
    def picker_next_index_for_column (column)
      picker_current_index_for_column(column) + 1
    end
    
    def picker_scroll_down_on_column(column)
      new_row = previous_index_for_column column
      query("pickerTableView index:'#{column}'", [{:selectRow => new_row},
                                                  {:animated => 1},
                                                  {:notify => 1}])
    end
    
    def picker_scroll_up_on_column(column)
      new_row = picker_next_index_for_column column
      query("pickerTableView index:'#{column}'", [{:selectRow => new_row},
                                                  {:animated => 1},
                                                  {:notify => 1}])
    end
    def visible_titles (column)
      query("pickerTableView index:#{column} child pickerTableViewWrapperCell", :wrappedView, :text).reverse
    end
    
    # May only work on circular pickers - does _not_ work on non-circular
    # pickers because the visible titles do _not_ follow the selected index
    def selected_title_for_column (column)
      selected_idx = picker_current_index_for_column column
      titles = visible_titles column
      titles[selected_idx]
    end
    
    def scroll_picker(dir, column)
      if dir.eql? 'down'
        picker_scroll_down_on_column column
      else
        picker_scroll_up_on_column column
      end
      sleep 0.4
    end