user89021 user89021 - 1 year ago 147
Ruby Question

How to upload a file with watir and IE?

I am writing a watir script to test an upload form.

But the script does not automatically choose the file that is to be uploaded from my harddrive.

Instead IE stops with the file chooser dialog open. As soon as I manually select the to be uploaded file in the dialog and click ok, watir continues as desired. I wonder why it stops.

This is my watir script:

require 'test/unit'
require 'watir'

# runs on win3k, IE 6.0.3790; ruby 1.8.6, watir

class EpcHomePage < Test::Unit::TestCase

def test_upload
ie = @browser
htmlfile = "C:\\testing\\upload.html"
uploadfile = "C:\\testing\\upload.html"
assert_equal uploadfile, ie.file_field(:name,"file1").value
ie.button(:name, 'upload').click

def setup
@browser =

def teardown

I got the code from this page:

This is the form:

<form name="form1" enctype="multipart/form-data" method="post" action="upload.html">
<input type="file" name="file1">
<input type="submit" name="upload" value="ok">

I have found this manual also. I am using IE 6 and also IE 7for the testing.

Edit: I have uploaded my simple example here (3 files that live in c:\testing\ on my machines, just start the cmd file):

It fails on 3 different machines (all windows 2003, 2x IE 6 and 1 x IE 7). I have also changed the sleep time in the script c:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.5\lib\watir\input_elements.rb from 1 second to 5 seconds, like suggested by Željko Filipin in his answer:

def set(path_to_file)
require 'watir/windowhelper'
begin do
sleep 5 # it takes some time for popup to appear
system %{ruby -e '

This is where it stops (please note that I did manually navigate to the directory in the file dialog once. From that point on IE always shows the open dialog with this directory, but that does not mean that the script selected the directory. I think it means that IE always shows the last directory where it left):

this is where it stops


I found that that the ole32 code looks for the english title:

POPUP_TITLES = ['Choose file', 'Choose File to Upload']

I installed IE 7 english version now. Still no success. But I think it has something to do with the localization, because input_elements.rb searches the window titles. I wonder why it still fails now. This is the code from input_elements.rb:

class FileField < InputElement
INPUT_TYPES = ["file"]
POPUP_TITLES = ['Choose file', 'Choose File to Upload']

# set the file location in the Choose file dialog in a new process
# will raise a Watir Exception if AutoIt is not correctly installed
def set(path_to_file)
require 'watir/windowhelper'
begin do
sleep 2 # it takes some time for popup to appear
system %{ruby -e '
require "win32ole"
@autoit ="AutoItX3.Control")
time =
while ( - time) < 15 # the loop will wait up to 15 seconds for popup to appear
#{POPUP_TITLES.inspect}.each do |popup_title|
next unless @autoit.WinWait(popup_title, "", 1) == 1
@autoit.ControlSetText(popup_title, "", "Edit1", #{path_to_file.inspect})
@autoit.ControlSend(popup_title, "", "Button2", "{ENTER}")
end # each
end # while
raise Watir::Exception::WatirException, "Problem accessing Choose file dialog"

The text "Choose file" now appears in the title of my new IE. Anything else that should be localized or changed here? I updated the screenshot to the english version.

Answer Source

I knew about that problem, and completely forgot! Go to input_elements.rb file in your gems directory, and add the title of the file upload window in your language to POPUP_TITLES (line 443).


  • before

    POPUP_TITLES = ['Choose file', 'Choose File to Upload']
  • after

    POPUP_TITLES = ['Choose file', 'Choose File to Upload', 'File upload in my language']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download