0

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.

Kin
  • 1
  • 3

1 Answers1

1

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
yoz
  • 532
  • 4
  • 11
  • 1
    The OP was asking for a way to get options _besides_ the first. Step 1: `selector = %Q(option[value="#{category_name}"])` Step 2: `@current_budget_page.categories.first(selector).select_option` – Tim Mulry Oct 13 '16 at 18:29
  • Hi Tim, I tried what you suggested but getting one error, any idea what's wrong? Step 1: `selector = %Q(option[text="Other"])` Step 2: `@page.option_elements.first(selector).select_optio‌​n` Error: `TypeError: no implicit conversion of String into Integer from (pry):2:in 'first'` – mickael May 30 '17 at 11:19