How to select values in UIPickerView

373 views
Skip to first unread message

Leif Bredgaard Andersen

unread,
Aug 2, 2011, 10:57:00 AM8/2/11
to Frank
Hi there

I have been struggling for the past couple of days figuring out how I
can select values in a UIPickerView using frank. Today I got a sort of
breakthrough but still not there.

I have defined following step:
When /^I select (\d*)(?:st|nd|rd|th)? row in picker "([^\"]*)"$/ do |
row_index, theview|
selector = "view:'UIPickerView' tag:'#{theview}'"
raise "could not find anything matching [#{theview}] to select" if
selector.empty?
row_index = row_index.to_i
sect_index = 0
views_switched = frankly_map( selector,
'selectRow:inComponent:animated:', row_index, sect_index, true )
raise "could not find anything matching [#{row_index}] to switch" if
views_switched.empty?
end

It seems to find the picker view all right but I will not select any
rows in it - at least it is not visible what it does. I invoke it from
my feature like this: When I select 2nd row in picker "PickerView"
PickerView is the values of the accessibility label of my
UIPickerView.

Hope somebody here can point me in the right direction :)

Best regards
Leif

Stewart Gleadow

unread,
Aug 2, 2011, 11:22:30 PM8/2/11
to frank-...@googlegroups.com
Hi Leif,

I haven't looked at this stuff for a while, but I wonder if you want to pass through '1' instead of 'true' for the animated flag since they need to map to Objective C types.

- Stew

Leif Bredgaard Andersen

unread,
Aug 3, 2011, 4:37:18 AM8/3/11
to Frank
Hi Stew

Thanks for the input. I tried it out but did not make any difference.
I was inspired by the code from When I flip a switch where true is
used. I will try some things about changing the value and see what I
might find :)

- Leif

Leif Bredgaard Andersen

unread,
Aug 3, 2011, 10:47:23 AM8/3/11
to Frank
Weee.... Finally I found a solution to this :)

I ended up adding following lines to the core_frank_steps.rb file:
# -- PickerView -- #

Then /^I should see picker "([^\"]*)"$/ do |expected_mark|
check_element_exists("view:'UIPickerView'
marked:'#{expected_mark}'")
end

When /^I select (\d*)(?:st|nd|rd|th)? row in picker "([^\"]*)"$/ do |
row_index, theview|
selector = "view:'UIPickerView' marked:'#{theview}'"
row_index = row_index.to_i
views_switched = frankly_map( selector,
'selectRow:inComponent:animated:', row_index, 0, false )
raise "could not find anything matching [#{row_index}] to switch" if
views_switched.empty?
end

What did I do?
1) The first issue was my selector. I used tag instead of marked. I
made the The I should see picker step to help figure out why it did
not worked and found it a handy step to have so I kept.
2) After I got the selector to work I got EOFError returned when I
called When I select step, which I could not quite figure out till I
ran the test while having the app in the debugger and found an
NSRangeException being thrown due to a pretty weird 8 digit index,
which of course is out range ;) I found a solution to this issue in
this post:
http://groups.google.com/group/frank-discuss/browse_thread/thread/0d5d2abfd4105bf1
Please notice the row_index = row_index.to_i - this is necessary in
order to convert the value to an integer, otherwise I will not work :)

Maybe this could a possible add-in to the official core_frank_steps.rb
file?

Best regards
Leif

On Aug 3, 10:37 am, Leif Bredgaard Andersen <lband...@acosys.dk>
wrote:

Pete Hodgson

unread,
Aug 4, 2011, 8:26:24 AM8/4/11
to frank-...@googlegroups.com
Glad you persevered and figured it out Leif :)

I took the liberty of adding this step definition to the wiki: https://github.com/moredip/Frank/wiki/Step-Definition-compendium

If someone wants to send me a pull request I'd be happy to add it to core_frank_steps.rb

Leif Bredgaard Andersen

unread,
Aug 4, 2011, 8:57:20 AM8/4/11
to Frank
Please do so Pete :)

I changed one thing though, which I was about to post now :)
I changed: row_index = row_index.to_i -> row_index =
row_index.to_i-1
This changed was to take care of the zero-based index of the
UIPickerView and make it more intuitive for test-writers :)


On Aug 4, 2:26 pm, Pete Hodgson <phodg...@thoughtworks.com> wrote:
> Glad you persevered and figured it out Leif :)
>
> I took the liberty of adding this step definition to the wiki:https://github.com/moredip/Frank/wiki/Step-Definition-compendium
>
> If someone wants to send me a pull request I'd be happy to add it to
> core_frank_steps.rb
>
> On Wed, Aug 3, 2011 at 10:47 AM, Leif Bredgaard Andersen <lband...@acosys.dk
> >http://groups.google.com/group/frank-discuss/browse_thread/thread/0d5...
Reply all
Reply to author
Forward
0 new messages