2014-02-14 41 views
3

選擇我有這個網站:選擇多個選項都與水豚黃瓜

<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 

回答

0

我發現....它是:

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

我有同樣的例子,所以我希望它可以幫助你

1)塞納里奧:

@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  |*  | 
  • 「分配給」是選擇標籤

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 
3

我能做到以下幾點:

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