2015-10-19 20 views
0

我已經定義了一個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 

,但是這不是我想要的。我認爲這是從列表中獲取第一個元素(默認值)。

我希望能夠在我的步驟中說: 從頁面上的下拉菜單中選擇一個特定元素。

有幫助嗎?

謝謝。

回答

0

需要兩個步驟:

  1. 找到你要找的<option>元素(在這種情況下,第一)。您可以在<select>節點上使用#first來完成此操作,因此:@current_budget_page.categories.first('option')
  2. 選擇它。您可以使用[#select_option] [1]。

所以,你想要的代碼是:

@current_budget_page.categories.first('option').select_option 
+0

的OP問的方式來獲得期權_besides_第一。第1步:'選擇器=%Q(選項[值=「#{category_name}」])步驟2:'@ current_budget_page.categories.first(選擇器).select_option' –

+0

您好,我嘗試了您的建議,錯誤,任何想法有什麼不對? 步驟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

相關問題