表格未填写 - Capybara

Form is not being filled - Capybara

具有这种形式:

<h3 class="contact-header"><span>Ready to try it</span> GET IN TOUCH</h3>
<%= form_for @contact, url: pages_path, remote: true do |f| %>
  <div class= "flex-row">
    <div class="form-input-spacing">
      <%= f.label :name %>
      <%= f.text_field  :name, required: true, class: "contact-form-text-area" %>
    </div>
    <div class="form-input-spacing">
      <%= f.label :email %>
      <%= f.text_field :email, required: true, class: "contact-form-text-area" %>
    </div>
  </div>
  <div class="area-input">
    <%= f.label :message %>
    <%= f.text_area :message, rows: 8, cols: 59, required: true, class: "contact-form-text-area",
                            placeholder: "Send us a message"%>
  </div>
  <div class="submit-form">
    <%= f.submit 'Send Message', class: 'btn btn-primary' %>
  </div>
<% end %>

我正在尝试测试它是如何被填充然后与 Capybara 一起发送的:

scenario "visitor/user sends incomplete contact form" do
    visit root_path #form is in root

    within(".new_contact") do
      # fill_in "#contact_name", with: "Test User"
      # fill_in "contact_name", with: "Test User"
      find("#contact_name").set("Test User")
    end

    save_and_open_page   
  end

我在场景中尝试了 js: truefill_infind 但是当我这样做时 save_and_open_page 什么都没有填充。

之所以用.new_contact是因为在Inspector中,那个s the class that the form takes, the #contact_nameis the id that the input takes andcontact_nameis the labelfor` .

如果我使用 click_button "Send Message" 按钮被点击并出现一条消息,那么为什么它得到的是按钮而不是输入?谢谢!

save_and_open_page 使用元素属性而不是元素的属性保存 HTML。它仅在您想要查看修改后的页面结构时才有用(HTML 结构已更改)。当您更改字段的内容时,它会更改该元素的值 属性 但实际上不会更新页面 HTML (值属性),因此当您保存 HTML 它会' 设置值。如果您想按原样查看页面,请使用 save_and_open_screenshot,或者暂停您的测试并查看浏览器。

另外 fill_in 需要一个定位器,而不是 CSS,所以它将是

fill_in "contact_name", with: "Test User"

来自 https://rubydoc.info/gems/capybara/Capybara/Node/Actions#fill_in-instance_method - The field can be found via its name, id, test_id attribute, placeholder, or label text. - 不是 CSS