Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
    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

I got the code from this page: http://wiki.openqa.org/display/WTR/File+Uploads

This is the form:

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

I have found this manual http://svn.openqa.org/svn/watir/trunk/watir/unittests/filefield_test.rb 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):

http://dl.dropbox.com/u/1508092/testing.rar

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)
      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 '
          ...

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 http://dl.dropbox.com/u/1508092/upload-dialog.JPG

Edit:

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)
      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) < 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

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.

like image 715
user89021 Avatar asked Apr 21 '10 23:04

user89021


2 Answers

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).

Example:

  • before

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

    POPUP_TITLES = ['Choose file', 'Choose File to Upload', 'File upload in my language']
    
like image 105
Željko Filipin Avatar answered Oct 12 '22 00:10

Željko Filipin


I installed windows xp in english now, and it works! (The error occured on a localised windows server 2003)

I guess it was the localisation issue. I will just run watir on the english computer from now on.

like image 21
user89021 Avatar answered Oct 12 '22 00:10

user89021