开发者

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.


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']
    


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.


I had the same problem today (March1,2012) and landed here via Google.

Thanks Željko for pointing me in the right direction, however the solution of changing the [POPUP_TITLES] didn't work. In fact, this array seems not to exist anymore in the current version of the gem (watir-2.0.4), or maybe I just misread.

I solved the problem in watir-2.0.4/lib/watir/dialogs/file_field.rb: Here, the various window and button titles are defined as regular expressions. Change the regexps in the following methods

  • open_button()
  • cancel_button()
  • file_upload_window()

to match your localized window names. After reloading the gem, it worked flawlessly.


I would suggest that you take a look at FileField#set in input_elements.rb (in your Ruby gems directory), and change sleep 1 to sleep 2 (or some higher number). I have noticed that on slower machines it takes more than a second for file upload pop up to appear.


  @modal = @browser.driver.switch_to.alert   #Switch to open windows modal
  key_to_send = "C:\\Users\\singhku\\Calabash_doc.pdf"  #Path and name of file
  @modal.send_keys(key_to_send)

  require 'win32ole'
  wsh = WIN32OLE.new('Wscript.Shell')
  wsh.AppActivate('Choose File to Upload')  #Name of the modal that is open
  wsh.SendKeys('{ENTER}')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜