Capybara Action整数(id)与字符串(name)的混淆

时间:2017-10-03 21:05:58

标签: ruby-on-rails capybara

我的部分测试需要从此表单字段中选择一所大学:

<div class="js-dependent-fields" data-select-id="user_role" data-option-value="staff">
  <div class="form-group">
    <%= form.label :university %>
    <%= form.select :university_id, options_for_select(University.all.map{ |uni| [uni.name, uni.id] }), {prompt: 'Select Your University'}, { class: "form-control" } %>
  </div>
</div>

这是实际的html:

<div class="js-dependent-fields" data-select-id="user_role" data-option-value="staff">
  <div class="form-group">
    <label for="user_university">University</label>
    <select class="form-control" name="user[university_id]" id="user_university_id"><option value="">Select Your University</option>
<option value="1">Harvard</option></select>
  </div>

以下是我遇到的错误:

  test_signup_valid_user                                         ERROR (0.05s)
Capybara::ElementNotFound:         Capybara::ElementNotFound: Unable to find visible option "Harvard University"
            test/integration/boarding_flow_test.rb:63:in `block in <class:BoardingFlowTest>'

提交表单时,这是它作为参数

的一部分传递的内容的示例
"university_id"=>"4"

在下面的测试中我想选择一所大学,并且我已经尝试了我能想到的每一种组合:

page.select "3", from: "university_id"
page.select "Harvard University", from: "University"

但我似乎无法让它发挥作用。这样做的正确方法是什么?谢谢!

test 'signup valid user' do
    visit sign_up_path

    fill_in "First name", with: "John"
    fill_in "Last name", with: "Doe"
    fill_in "Email", with: "approved@doe.com"
    fill_in "Password", with: "foobar"
    page.select "Staff", from: "Role"
    page.select "3", from: "university_id"

    click_on "Signup"

    assert_equal sign_in_path, current_path
    assert page.has_content?("confirm your email")
  end

以下是users / new.html.erb

<div class="row">
  <div class="col-sm-8 col-md-4 col-sm-offset-2 col-md-offset-4">
    <div class="login-panel panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Signup</h3>
      </div>
      <div class="panel-body">
        <%= form_for @user do |form| %>
          <fieldset>
            <%= render partial: '/users/form', object: form %>

            <%= form.submit t(".submit"), class: "btn btn-md btn-success btn-block" %>
            <hr>
            <h5 class="text-center">Already have an account?</h5>
            <%= link_to t(".login"), sign_in_path, class: "btn btn-md btn-info btn-block" %>
          </fieldset>
        <% end %>
      </div>
    </div>
  </div>
</div>

此处为部分

<div class="form-group">
  <%= form.label :first_name %>
  <%= form.text_field :first_name, class: "form-control" %>
</div>

<div class="form-group">
  <%= form.label :last_name %>
  <%= form.text_field :last_name, class: "form-control" %>
</div>

<div class="form-group">
  <%= form.label :email %>
  <%= form.text_field :email, type: 'email', class: "form-control" %>
</div>

<div class="form-group">
  <%= form.label :password %>
  <%= form.password_field :password, class: "form-control" %>
</div>

<div class="form-group">
  <%= form.label :role %>
  <%= form.select :role, options_for_select(User.roles.keys.map{ |role| [User.human_attribute_name("role.#{role}"), role] }), {}, { class: "form-control" } %>
</div>

<div class="js-dependent-fields" data-select-id="user_role" data-option-value="staff">
  <div class="form-group">
    <%= form.label :university %>
    <%= form.select :university_id, options_for_select(University.all.map{ |uni| [uni.name, uni.id] }), {prompt: 'Select Your University'}, { class: "form-control" } %>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

使用page.select时 - 使用&#39;选项匹配主要参数&#39;选择器类型 - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selector.rb#L413 - 并使用&#39;选择&#39;匹配:from选项选择器类型 - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selector.rb#L358。这意味着:from选项将匹配HTML <select>元素的名称,ID,占位符或关联标签文本,并且main参数将与可选选项的文本匹配。您无法显示该网页的实际HTML或您正在调用的form对象的详细信息&#39; form.select&#39; on - 但如果它是用户对象,则可能类似于

page.select("Harvard", from: 'user_university_id') # assuming the id of the actual select is 'user_university_id'

如果页面上只有一个选项可以选择&#34;哈佛大学&#34;,你也可以这样做

page.select "Harvard"

您尝试无效的原因是因为 -

# Doesn't work because "3" isn't the readable text of the option and
# 'university_id' is not the id of the select element
page.select "3", from: "university_id"

# Doesn't work because "University" is not the label text associated
# with the select.  This is because your partials label line would need to be
#   form.label :university_id, "University"
# to link the label with the university_id select element.  Additionally 
# "Harvard University" isn't actually the text shown in your HTML - it's just "Harvard"
page.select "Harvard University", from: "University"

这一切都假设页面上的可见元素实际上是<select>元素,而不是JS驱动的小部件(包装div上的js-dependent-fields类倾向于指示它可能是)。如果可见元素是JS小部件,那么这一切都会从窗口中消失,因为page.select仅适用于HTML <select>元素,您需要显示可见选择小部件的实际HTML在你的页面上。