当id是动态使用页面对象时,如何查找select_list选择的选项

时间:2014-01-03 20:02:14

标签: ruby watir-webdriver page-object-gem

当一个项目被添加到购物车时,我将在div中有五个select_lists,我需要获得使用page-object gem的选定选项。

代码如下所示:

<div id='item_basket_235423423'>
  <select_list id = 'fruit_23423423'>...</select_list>
  <select_list id = 'fruit_24533424'>...</select_list>
  <select_list id = 'fruit_26352321'>...</select_list>
  <select_list id = 'fruit_23462425'>...</select_list>
  <select_list id = 'fruit_23346342'>...</select_list>
</div>
<div id='item_basket_23423464'>
  <select_list id = 'veg_684341'>...</select_list>
  <select_list id = 'veg_65181351'>...</select_list>
  <select_list id = 'veg_86516843'>...</select_list>
  <select_list id = 'veg_21676919'>...</select_list>
  <select_list id = 'veg_9831568'>...</select_list>
</div>

找到所有select_lists:

select_lists(:selected_basket_fruits, :id => /fruit/)
select_lists(:selected_basket_vegetables, :id => /veg/)

从这里开始,我不确定如何为每个选项找到所选的选项。我使用ruby / cucumber / watir-webdriver和页面对象gem。

一如既往,感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

访问者:

select_lists(:selected_basket_fruits, :id => /fruit/)

将定义一个返回匹配选择列表集合的方法:

selected_basket_fruits_elements

这是enumerable,因此您可以使用each等方法对其进行迭代。迭代时,您可以使用选择列表的selected_options方法来确定所选的选项。

page.selected_basket_fruits_elements.each do |select_list|
  p select_list.selected_options
end

请注意selected_options会返回一系列选定的选项。