Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
759 views
in Technique[技术] by (71.8m points)

ruby - 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:esting 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: ubylib ubygems1.8gemswatir-1.6.5libwatirinput_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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...