选择多种选择中的所有选项与capybara-cucumber

时间:2014-02-14 19:38:27

标签: ruby cucumber capybara

我有这个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>

我正在尝试用黄瓜来选择所有值,但它没有运行。这些是我的尝试:

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

3 个答案:

答案 0 :(得分:7)

我能够做到以下几点:

select = page.find('select#select_id')
select.select 'Option 1'
select.select 'Option 2'
select.select 'Option 3'
select.unselect 'Option 1'

答案 1 :(得分:0)

我发现......它是:

page.all(:xpath, "//select[@id='" + select_id + "']/option").each do |e|
        e.click
    end

答案 2 :(得分:0)

我有相同的例子,所以我希望它可以帮助你

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        |*      |
  • &#34;分配给&#34;是选择标记

2)步骤定义

  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