3

I have this html:

<select id="id_agents" style="" size="10" multiple="multiple" name="id_agents[]">
<option value="12">adama</option>
<option value="15">artica</option>
<option value="14">localhost</option>
<option value="8">localhost.localdomain</option>
<option value="13">test</option>
</select>

And I am trying with cucumber to select all values but it is not running. These are my tries:

When /^I select all in "(.*)"/ do |select_id|
    options = all(:xpath, "//select[@id='" + select_id + "']/option").click
    options.each do |option|
        option.click
    end
    #~ find(:xpath, "//select[@id='" + select_id + "']/option").each do |element|
        #~ element.click
    #~ end
    sleep(10)
end
tres.14159
  • 850
  • 1
  • 16
  • 26

3 Answers3

8

I was able to do the following:

select = page.find('select#select_id')
select.select 'Option 1'
select.select 'Option 2'
select.select 'Option 3'
select.unselect 'Option 1'
olleicua
  • 2,039
  • 2
  • 21
  • 33
0

I found....it is:

page.all(:xpath, "//select[@id='" + select_id + "']/option").each do |e|
        e.click
    end
tres.14159
  • 850
  • 1
  • 16
  • 26
0

I have the same example so I hope it helps you

1) Senario:

    @create-product-backlog-with-multi-assign-to
    Scenario: Create a product backlog successfully
    Given I am on the new product backlog page
    #Then show me the page
    When I fill in a form with the following multiple values:
    |Project Title   |Project 1        |
    |Type            |Features         |
    |Title           |Product Backlog 8|
    |Priority        |Urgent           |
    # Product Backlog must have "State" is "Not Started" initially
    |State           |Not Started      |
    |Description     |description 8    |
    |Assign To       |developer,tester |
    |Estimated Hours | 48              |
    |Business Value  | 1               |
    |ROI             |0.02             |

    #|Attachment files|                 |
    # ROI is calculated from Business Value/Estimated Hours and this field is disable
    And I click on "Save" button
    Then I should be redirected to the product backlog index page
    #When I wait 5 seconds
    #When I select "All sprints" from "search_sprint_id"
    Then show me the page
    #When I wait 5 seconds

    # product backlog will assign to current user by default
    Then I should see a table with the following rows
    |ID|Title            |Priority|ROI  |State        |Assign To             |Estimated |Actions|
    |* |Product Backlog 8| Urgent |0.02 |Not Started  |admin,developer,tester|48        |*      |
  • "Assign To" is select tag

2) Step definitions

  When(/^I fill in a form with the following multiple values:$/) do |table|
     expectations = table.raw
     expectations.each do |label, expected_value|
       field =  find_field("#{label}")

       case field.tag_name
       when 'select'
          expected_value.split(',').each do |value|
             step %(I select "#{value}" from "#{label}")
          end
       when 'checkbox','radio'
          step %(I check "#{label}") unless expected_value.nil?
       else
         step %(I fill in "#{label}" with "#{expected_value}")
      end
    end
 end 
Hai Nguyen
  • 458
  • 9
  • 15