4
votes

My system:

Windows 10 Pro 64-bit

ruby 2.1.9p490 (2016-03-30 revision 54437) [x64-mingw32]

FireFox 47.0.1

To start, here is the code I'm dealing with:

<div class="dz-style col-sm-7" is="null">
  <div is="null">You can drag and drop your supporting document files here, or click to select files to upload.</div>
  <input style="display: none;" multiple="" is="null" type="file"></div>

Here is my watir testing code: Identify and confirm file is valid

local_file = '/Users/tom.feodoroff/Desktop/Charlie_Snoopy.jpg'

File.exists? local_file

raise "error" unless File.exists? local_file

Change the style display so I can interact with control

element = BROWSER.input(:type => 'file')

puts element.attribute_value('style') #display: none;

script = "return arguments[0].style = 'display: inline'"

BROWSER.execute_script(script, element)

puts element.attribute_value('style') #display: inline;

Use suggested syntax to add file to application

BROWSER.file_field(:type => 'file').set(local_file)

This doesn't generate any errors, but it also doesn't attach the file so that my Submit button becomes active. Do I need a different version of Ruby (Watir) to make this work or is there something I'm missing?

1
Try specifying the drive letter in local_file (e.g. C:) - orde
The code that activates the Submit button might not be listening for changes to the file field. The button is likely activated in the code that handles the dropped file, in which case you would have to find and manually trigger that as well. - Justin Ko
Thanks @orde, I added the drive to the file name: irb(main):001:0> local_file = 'C:/Users/tom.feodoroff/Desktop/Charlie_Snoopy.jpg' => "C:/Users/tom.feodoroff/Desktop/Charlie_Snoopy.jpg" irb(main):002:0> File.exists? local_file => true but that didn't solve my problem :( - Tom Feodoroff
Thanks @JustinKo good point, I'll check for that, but when I add the file manually to the application, I see the file icon on the page. When I do this through Watir, I don't see the icon for the file. Does Watir add these files without showing on the client side that they're added? - Tom Feodoroff
@TomFeodoroff, I am not exactly sure I understand what you mean by "file icon". However, I would guess that the answer is no. Watir just inputs the file field. Any display changes in the page would be due to the application detecting and then performing changes. - Justin Ko

1 Answers

1
votes

I don't understand why, but I added sleep(5) just before clicking the submit button, and now it works. My file icon now appears on the page and the submit button is active and successfully submitted the form. Things that make you go 'hmmmm' :) Thanks for the responses. Hopefully this will help someone else with this problem?