今天用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中
- class FileField < InputElement
- #:stopdoc:
- INPUT_TYPES = ["file"]
- POPUP_TITLES = ['Choose file', 'Choose File to Upload']
- #:startdoc:
- # set the file location in the Choose file dialog in a new process
- # will raise a WatirException 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 1 # 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
这里有 POPUP_TITLES = ['Choose file', 'Choose File to Upload'],表示是file upload这个控件的文字,在英语中是Choose file和Choose File to Upload而在汉语环境中的是浏览,我尝试过了再这个数组中添加'浏览'但是不管用修改中间的“choose file”成“选择文件”(其中“选择文件”是根据自己操作系统的弹出对话框的title来决定的。因为有些即使是英文操作系统但是“choose file to upload”就需要也进行修改。
转载于:https://blog.51cto.com/visionsky/421955