Tom Feodoroff
Tom Feodoroff

Reputation: 71

Problems uploading a file using file_field in Watir

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?

Upvotes: 4

Views: 626

Answers (1)

Tom Feodoroff
Tom Feodoroff

Reputation: 71

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?

Upvotes: 1

Related Questions