Kin
Kin

Reputation: 1

How to use SitePrism gem to select an option from a dropdown menu and test that the option has been selected

I have defined a SitePrism page that has a select tag with options (to create a dropdown menu on my page).

In my cucumber steps, I'm trying to set it so that it selects one of the options from the dropdown menu but it seems impossible to do so.

Here is my setup:

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']"

In my steps I have:

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

And my page looks like:

<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>

The problem is that calling "select" on the element in the steps does not work! It complains with: ArgumentError Exception: wrong number of arguments (1 for 0)

Calling select without argument works does not complain

@current_budget_page.categories.select

, but that's not what I want. I think that's getting the first element from the list (the default value).

I want to be able to say in my steps: select a particular element from the dropdown menu on the page.

Any helps, please?

Thanks.

Upvotes: 0

Views: 1138

Answers (1)

yoz
yoz

Reputation: 542

EDIT: My apologies: Tim Mulry is correct, and I misread the question. To select items other than the first, see his answer in the comments.

ORIGINAL:

The two steps you need are:

  1. Find the <option> element you're looking for (in this case, the first). You can do this with #first on the <select> node, so: @current_budget_page.categories.first('option')
  2. Select it. You can use [#select_option][1] for this.

So the code you want is:

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

Upvotes: 1

Related Questions