简体   繁体   English

将select_list选项转换为watir-webdriver中的字符串数组?

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

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; 我正在将有效选项读入CVS字段中的字符串数组,并通过执行以下操作进行比较;

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? 这是正确的还是现有的select_list方法进行类似的转换?

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)

Have you tried the .options method? 你试过.options方法吗? If I'm reading the RDOC for Watir-webdriver correctly, it should return a collection with all the options in the select list. 如果我正在正确读取Watir-webdriverRDOC ,它应该返回一个包含选择列表中所有选项的集合。

An alternate way to do this using loops instead of .map is: 使用循环而不是.map执行此操作的另一种方法是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM