Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to upload a file with watir and IE?
    text
    copied!<p>I am writing a watir script to test an upload form.</p> <p>But the script does not automatically choose the file that is to be uploaded from my harddrive.</p> <p>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.</p> <p>This is my watir script:</p> <pre><code>require 'test/unit' require 'watir' # runs on win3k, IE 6.0.3790; ruby 1.8.6, watir class EpcHomePage &lt; Test::Unit::TestCase def test_upload ie = @browser htmlfile = "C:\\testing\\upload.html" uploadfile = "C:\\testing\\upload.html" ie.goto(htmlfile) ie.file_field(:name,"file1").set(uploadfile) assert_equal uploadfile, ie.file_field(:name,"file1").value ie.button(:name, 'upload').click end def setup @browser = Watir::IE.new end def teardown @browser.close end end </code></pre> <p>I got the code from this page: <a href="http://wiki.openqa.org/display/WTR/File+Uploads" rel="noreferrer">http://wiki.openqa.org/display/WTR/File+Uploads</a> </p> <p>This is the form:</p> <pre><code>&lt;html&gt;&lt;body&gt; &lt;form name="form1" enctype="multipart/form-data" method="post" action="upload.html"&gt; &lt;input type="file" name="file1"&gt; &lt;input type="submit" name="upload" value="ok"&gt; &lt;/form&gt; &lt;/body&gt;&lt;/html&gt; </code></pre> <p>I have found this manual <a href="http://svn.openqa.org/svn/watir/trunk/watir/unittests/filefield_test.rb" rel="noreferrer">http://svn.openqa.org/svn/watir/trunk/watir/unittests/filefield_test.rb</a> also. I am using IE 6 and also IE 7for the testing.</p> <p><em>Edit:</em> I have uploaded my simple example here (3 files that live in c:\testing\ on my machines, just start the cmd file):</p> <p><a href="http://dl.dropbox.com/u/1508092/testing.rar" rel="noreferrer">http://dl.dropbox.com/u/1508092/testing.rar</a></p> <p>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:</p> <pre><code> def set(path_to_file) assert_exists require 'watir/windowhelper' WindowHelper.check_autoit_installed begin Thread.new do sleep 5 # it takes some time for popup to appear system %{ruby -e ' ... </code></pre> <p>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):</p> <p><a href="http://dl.dropbox.com/u/1508092/upload-dialog.JPG" rel="noreferrer">this is where it stops http://dl.dropbox.com/u/1508092/upload-dialog.JPG</a></p> <p><em>Edit:</em></p> <p>I found that that the ole32 code looks for the english title:</p> <p>POPUP_TITLES = ['Choose file', 'Choose File to Upload']</p> <p>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:</p> <pre><code> class FileField &lt; 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) assert_exists require 'watir/windowhelper' WindowHelper.check_autoit_installed begin Thread.new do sleep 2 # it takes some time for popup to appear system %{ruby -e ' require "win32ole" @autoit = WIN32OLE.new("AutoItX3.Control") time = Time.now while (Time.now - time) &lt; 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}") exit end # each end # while '} end.join(1) rescue raise Watir::Exception::WatirException, "Problem accessing Choose file dialog" end click end end </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload