【问题标题】:Select all options in multiple select with capybara-cucumber使用 capybara-cucumber 在多项选择中选择所有选项
【发布时间】:2015-08-04 01:18:36
【问题描述】:

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

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

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

【问题讨论】:

    标签: ruby cucumber capybara


    【解决方案1】:

    我能够做到以下几点:

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

    【讨论】:

      【解决方案2】:

      我找到了……它是:

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

      【讨论】:

        【解决方案3】:

        我有同样的例子,希望对你有所帮助

        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 
        

        【讨论】:

        • 如果选项文本包含,怎么办?
        • 你可以用逗号分割它并循环它们,就像'select'的情况一样。这取决于您的具体情况,因此您可以根据自己的目的进行适当定制
        • 那么您的解决方案不能适用于一般情况。必须逐个更改代码。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-15
        • 1970-01-01
        • 2013-03-23
        • 2015-02-13
        • 2011-05-27
        相关资源
        最近更新 更多