我正在编写一个 watir 脚本来测试上传表单。
但是脚本不会自动选择要从我的硬盘上载的文件。
相反,IE 在文件选择器对话框打开时停止。只要我在对话框中手动选择要上传的文件并单击确定,watir 就会根据需要继续。我想知道为什么它会停止。
这是我的女仆脚本:
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
我从这个页面得到了代码:http ://wiki.openqa.org/display/WTR/File+Uploads
这是表格:
<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>
我也找到了本手册http://svn.openqa.org/svn/watir/trunk/watir/unittests/filefield_test.rb。我正在使用 IE 6 和 IE 7 进行测试。
编辑:我在这里上传了我的简单示例(3 个文件位于我的机器上的 c:\testing\ 中,只需启动 cmd 文件):
http://dl.dropbox.com/u/1508092/testing.rar
它在 3 台不同的机器上失败(所有 Windows 2003、2x IE 6 和 1x IE 7)。我还将脚本 c:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.5\lib\watir\input_elements.rb 中的睡眠时间从 1 秒更改为 5 秒,就像 Željko Filipin 所建议的那样在他的回答中:
    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 '
          ...
这是它停止的地方(请注意,我确实手动导航到文件对话框中的目录一次。从那时起,IE 总是显示带有该目录的打开对话框,但这并不意味着脚本选择了该目录。我认为这意味着 IE 总是显示它离开的最后一个目录):
这是它停止的地方 http://dl.dropbox.com/u/1508092/upload-dialog.JPG
编辑:
我发现 ole32 代码寻找英文标题:
POPUP_TITLES = ['选择文件', '选择要上传的文件']
我现在安装了 IE 7 英文版。仍然没有成功。但我认为这与本地化有关,因为 input_elements.rb 搜索窗口标题。我想知道为什么它现在仍然失败。这是 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
文本“选择文件”现在出现在我的新 IE 的标题中。还有什么应该在这里本地化或更改的吗?我将屏幕截图更新为英文版。