Phil
Phil

Reputation: 916

Can I get attach_file to work without an ID or class name?

The markup looks exactly like this:

<input type="file">

Is there any way to get attach_file to work with this?

In the meantime I've asked a dev to add an id for me, but I try to avoid any special asks altogether.

Upvotes: 0

Views: 378

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

Capybara's #attach_field basically boils down to find(:file_field, locator, options).set(path) which will find by id, name, or associated label text. However if you find the element another way you can just call #set on it with the path. Another thing to note is that (with up to date versions of Capybara) if you only have one file field on the page or you are scoped to a region of the page that only has one file field you can pass nil as the locator and it will just find any input of type 'file'.

So

attach_file(nil, 'test.txt')

is equivalent to

find('input[type="file"]').set('text.txt')

except that the example with set will skip Capybara's existence test for the file

Upvotes: 2

Related Questions