Turning a select_list options into a string array in watir-webdriver?
Asked Answered
G

3

2

I need to check the contents of a select list drop down which varies depending on a value in another field. I am reading the valid options into an array of strings from a CVS field and comparing by doing the following;

selectContent = []
$browser.select_list(:id,"srch-status-select").options.each {|option| selectContent << option.text}
assert_equal(validContent,selectContent,"Status drop down has wrong values")

Is this correct or is there an existing select_list method which does a similar conversion?

Ghetto answered 15/6, 2011 at 13:57 Comment(0)
S
11

There's no method doing exactly what you want, but a more concise version would be:

selectList = $browser.select_list(:id,"srch-status-select")
selectContent = selectList.options.map(&:text)
Suppress answered 15/6, 2011 at 14:3 Comment(5)
Getting this error when I try that code, TypeError: wrong argument type Symbol (expected Proc)Ghetto
Then you're using an old version of Ruby that does not support the symbol to proc-notation. You then need to replace (&:text) with {|option| option.text} ... or update Ruby.Suppress
I am running Ruby version 1.8.6 as that is recommended for Watir. Will try your suggested correction, thanks again.Ghetto
That may be the case for Watir, but watir-webdriver runs on Ruby 1.8.6 through 1.9.2.Frederik
Very timely. I ended up using this today.Efface
S
2

Have you tried the .options method? If I'm reading the RDOC for Watir-webdriver correctly, it should return a collection with all the options in the select list.

Sennet answered 16/6, 2011 at 8:7 Comment(3)
It does that; but I need them as an array of text values, instead of an area of option elements which it returns.Ghetto
if you do .text on the option elements does it return the display text for the options? if so can't you just iterate through the the array of options and assign the .text of each one to an element in another array? Um wait, I think that's what Magnar's answer is doingSennet
It is. you can do selectList.options.map{|o| o.text} for an array of entries.Soulsearching
B
0

An alternate way to do this using loops instead of .map is:

elems = Array.new
values = Array.new
elems = @b.select_list(:id => "selectListId").options
0.upto(elems.length - 1) do |i|
    values.push elems[i].text
end

then to display the options

0.upto(values.length - 1) do |i|
    puts values[i]
end
Brag answered 7/2, 2012 at 1:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.