I'm having trouble attaching a file using Minitest with Capybara in Rails. The problem is that the file field is in a nested form. The nested form gets a random ID and name added to it, so I can't select by these.
Normally when it's not a nested form this works:
attach_file("post[featured_image]", "#{Rails.root.join("test/fixtures/files/example-featured-image.jpg")}")
But, on the nested form the form field looks like this:
I thought this should work:
within(".image-upload") do
attach_file('input[type="file"]', "#{Rails.root.join("test/fixtures/files/example-featured-image.jpg")}", make_visible: true)
end
But that gives me this error:
Minitest::UnexpectedError: Capybara::ElementNotFound: Unable to find file field "input[type=\"file\"]" that is not disabled within #<Capybara::Node::Element tag="fieldset"
I've also tried other methods described in the capybara docs here.
Is there a way to do this? The only time I've gotten capybara attach_file
to work is when I've been able to target the element name as in the post[featured_image
example above.