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

折月煮酒 提交于 2020-01-12 10:47:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!