我有以下表格:
<%= f.hidden_field :document_type, value: item %>
<%= f.label :file, 'Upload', class: "btn btn-outline-primary btn-sm custom-btn-sm", id: "upload_kyc_#{idx}", name: "upload_button" %>
<%= f.file_field :file, class: "inputfile", accept: "jpeg, .jpg, .png, .pdf", onchange: 'this.form.requestSubmit()' %>
custom_styles.scss
.inputfile {
width: 0.1px;
height: 0.1px;
opacity: 0;
overflow: hidden;
position: absolute;
z-index: -1;
}
這在瀏覽器中產生了我:

現在我想用 Capybara 來測驗一下。所以我所做的是:
def pdf_path
"#{Rails.root}/spec/fixtures/files/sample.pdf"
end
it 'show message' do
login_as user
visit profile_path
attach_file('upload_kyc_0', pdf_path, visible: true)
end
但我得到一個錯誤:
Capybara::ElementNotFound:
Unable to find file field "inputfile" that is not disabled within #<Capybara::Node::Element tag="div" path="/HTML/BODY[1]/DIV[1]/DIV[1]/DIV[2]">
那么如何提交該檔案呢?
uj5u.com熱心網友回復:
將塊傳遞給attach_file用戶將要上傳的操作
attach_file(pdf_path) do
find(:label, 'Upload').click
end
uj5u.com熱心網友回復:
和:
attach_file('upload_kyc_0', pdf_path, visible: true)`
您正在嘗試附加到標簽,而不是檔案輸入。它應該是:
attach_file('file', pdf_path, visible: true)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/527871.html
