【问题标题】:Cucumber how to simulate multiple drop down selection?黄瓜如何模拟多个下拉选择?
【发布时间】:2012-03-14 17:39:40
【问题描述】:

您好,我是 Cucumber 和 Ruby on Rails 的新手,我不知道如何模拟多个下拉菜单选择。我可以为单个下拉菜单(在我的情况下为“评分”)做到这一点。

Given /the following movies exist:$/ do |movies_table|
  movies_table.hashes.each do |movie|
    # each returned element will be a hash whose key is the table header.
    # you should arrange to add that movie to the database here.
  Given %Q{I am on the Create New Movie page}
  When  %Q{I fill in "Title" with "#{movie[:title]}"}
  And   %Q{I select "#{movie[:rating]}" from "Rating"}
  And   %Q{I select "#{movie[:release_date]}" from "Released On"}
  And   %Q{I press "Save Changes"}
  end
  assert false, "Unimplmemented"
end

我认为与我的问题相关的错误是。

cannot select option, no select box with id, name, or label 'Released On' found (Capybara::ElementNotFound)
      (eval):2:in `select'
      ./step_definitions/web_steps.rb:86:in `/^(?:|I )select "([^"]*)" from "([^"]*)"$/'
      filter_movie_list.feature:9:in `Given the following movies exist:'

我认为我的网络应用程序中的相关源代码是

    <label for="movie_title">Title</label>
    <input id="movie_title" name="movie[title]" size="30" type="text" />
    <label for="movie_rating">Rating</label>
    <select id="movie_rating" name="movie[rating]"><option value="G">G</option>
    <option value="PG">PG</option>
    <option value="PG-13">PG-13</option>

    <option value="R">R</option>
    <option value="NC-17">NC-17</option></select>
    <label for="movie_release_date">Released On</label>
    <select id="movie_release_date_1i" name="movie[release_date(1i)]">
    <option value="2007">2007</option>
    <option value="2008">2008</option>

    < And so on ...>

    </select>
    <select id="movie_release_date_3i" name="movie[release_date(3i)]">
    <option value="1">1</option>

    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>

    < And so on ...>

    </select>
    <input name="commit" type="submit" value="Save Changes" />

所以我提到了“已发布”标签,但我的错误消息说它没有选择框,但它适用于评级。同样来自我的 .feature 文件的形式为

25-Nov-1992

如果它发挥了作用。但我认为不是,因为我认为 ruby​​ 了解如何处理日期。感谢您的宝贵时间。

【问题讨论】:

    标签: ruby ruby-on-rails-3 drop-down-menu cucumber capybara


    【解决方案1】:

    问题是 Released On 标签有 for="movie_release_date" 而选择框有 id="movie_release_date_1i"forid 值必须匹配,Capybara 才能根据标签文本定位字段。

    可能的解决方案:

    1) 如果您可以控制 HTML,请更正 HTML。

    你想拥有:

    <label for="movie_release_date">Released On</label>
    <select id="movie_release_date" name="movie[release_date(1i)]">
    

    <label for="movie_release_date_1i">Released On</label>
    <select id="movie_release_date_1i" name="movie[release_date(1i)]">
    

    2) 按 ID 或名称访问选择字段。

    And   %Q{I select "#{movie[:release_date]}" from "movie_release_date_1i"}
    

    And   %Q{I select "#{movie[:release_date]}" from "movie[release_date(1i)]"}
    

    此选项仅在 ID 或名称是静态的(即末尾的数字始终相同,而不是每次页面加载时随机生成)时才有效。

    3) 直接使用 Capybara 而不是使用 web_steps.rb

    如果您直接使用 Capybara,而不是使用 web_steps.rb(我假设您的步骤来自),您可以获得更多用于定位选择字段的选项(例如 xpath)。

    【讨论】:

      猜你喜欢
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      相关资源
      最近更新 更多