如何使用SitePrism gem从下拉菜单中选择一个选项并测试该选项已被选中
问题描述:
我已经定义了一个SitePrism页面,该页面有一个带选项的选择标签(用于在我的页面上创建一个下拉菜单)。如何使用SitePrism gem从下拉菜单中选择一个选项并测试该选项已被选中
在我的黄瓜步骤中,我试图设置它,以便从下拉菜单中选择其中一个选项,但似乎无法这样做。
这里是我的设置:
class ViewBudgetPage < SitePrism::Page
set_url '/budget{/id}'
element :date, "input[name='cost_date']"
element :description, "input[name='cost_desc']"
element :price, "input[name='cost_price']"
elements :categories, "select[id='categories_list']"
element :add_cost_button, "button[type='submit']"
在我的步骤,我有:
When "I fill in the form to create a new cost" do
@current_budget_page.date.set '1-Aug-2010'
@current_budget_page.description.set 'some description'
@current_budget_page.price.set '10'
@current_budget_page.categories.select "Donations"
end
而且我的网页看起来像:
<div class='form-group'>
<select class="form-control" id="categories_list"name='model_number'><option value='unselected'> Please select a category </option>
<% @budget_wrapper.all_categories.each do |cat_name| %>
<option value='<%=cat_name%>'><%=cat_name%></option>
<%end%>
</select>
</div>
的问题是,调用 “中选择”在步骤中的元素不起作用! ArgumentError异常:它与抱怨错误的参数数目(1 0)
不用争论的作品调用选择不抱怨
@current_budget_page.categories.select
,但是这不是我想要的。我认为这是从列表中获取第一个元素(默认值)。
我希望能够在我的步骤中说: 从页面上的下拉菜单中选择一个特定元素。
有帮助吗?
谢谢。
答
需要两个步骤:
-
找到你要找的
<option>
元素(在这种情况下,第一)。您可以在<select>
节点上使用#first
来完成此操作,因此:@current_budget_page.categories.first('option')
-
选择它。您可以使用[
#select_option
] [1]。
所以,你想要的代码是:
@current_budget_page.categories.first('option').select_option
的OP问的方式来获得期权_besides_第一。第1步:'选择器=%Q(选项[值=“#{category_name}”])步骤2:'@ current_budget_page.categories.first(选择器).select_option' –
您好,我尝试了您的建议,错误,任何想法有什么不对? 步骤1:'selector =%Q(option [text =“Other”])步骤2:'@ page.option_elements.first(selector).select_optio n' 错误:'TypeError:no implicit conversion of将字符串转换为整数 from(pry):2:in'first'' – mickael