关于Watir的upload file不能自动选择文件的解决方案

 今天用watir上传文件的附件时,按照http://wiki.openqa.org/display/WTR/File+Uploads的说明怎么都上传不了总是卡在选择文件的那块,在http://stackoverflow.com/questions/2687293/how-to-upload-a-file-with-watir-and-ie看到有人跟我遇到一样的问题,原来是本地化的问题,因为在input_elements.rb中

  
  
  
  
  1. class FileField < InputElement 
  2.    #:stopdoc: 
  3.    INPUT_TYPES = ["file"
  4.    POPUP_TITLES = ['Choose file''Choose File to Upload'
  5.    #:startdoc: 
  6.     
  7.    # set the file location in the Choose file dialog in a new process 
  8.    # will raise a WatirException if AutoIt is not correctly installed 
  9.    def set(path_to_file) 
  10.      assert_exists 
  11.      require 'watir/windowhelper' 
  12.      WindowHelper.check_autoit_installed 
  13.      begin 
  14.        Thread.new do 
  15.          sleep 1 # it takes some time for popup to appear 
  16.  
  17.          system %{ruby -e ' 
  18.              require "win32ole" 
  19.  
  20.              @autoit = WIN32OLE.new("AutoItX3.Control"
  21.              time    = Time.now 
  22.  
  23.              while (Time.now - time) < 15 # the loop will wait up to 15 seconds for popup to appear 
  24.                #{POPUP_TITLES.inspect}.each do |popup_title| 
  25.                  next unless @autoit.WinWait(popup_title, "", 1) == 1 
  26.  
  27.                  @autoit.ControlSetText(popup_title, """Edit1"#{path_to_file.inspect}) 
  28.                  @autoit.ControlSend(popup_title, """Button2""{ENTER}"
  29.                  exit 
  30.                end # each 
  31.              end # while 
  32.          '} 
  33.        end.join(1) 
  34.      rescue 
  35.        raise Watir::Exception::WatirException, "Problem accessing Choose file dialog" 
  36.      end 
  37.      click 
  38.    end 
  39.  end 

 这里有 POPUP_TITLES = ['Choose file''Choose File to Upload'],表示是file upload这个控件的文字,在英语中是Choose file和Choose File to Upload而在汉语环境中的是浏览,我尝试过了再这个数组中添加'浏览'但是不管用修改中间的“choose file成“选择文件”(其中“选择文件”是根据自己操作系统的弹出对话框的title来决定的。因为有些即使是英文操作系统但是“choose file to upload”就需要也进行修改。

你可能感兴趣的:(upload,File,职场,watir,休闲,选择文件)