问题
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?
回答1:
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)
回答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.
回答3:
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
来源:https://stackoverflow.com/questions/6358860/turning-a-select-list-options-into-a-string-array-in-watir-webdriver