如何使用watir和IE上传文件?

我正在编写一个watir脚本来测试上传表单。

但是脚本不会自动选择要从我的硬盘上传的文件。

相反,IE会在文件选择器对话框打开时停止。 只要我在对话框中手动选择要上传的文件并单击“确定”,watir就会根据需要继续。 我想知道为什么它会停止。

这是我的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

这是forms:

  

我也找到了这本手册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和1 x IE 7)。 我也在脚本c:\ ruby​​ \ lib \ ruby​​ \ gems \ 1.8 \ gems \ watir-1.6.5 \ lib \ watir \ input_elements.rb中将睡眠时间从1秒改为5秒,如ŽeljkoFilipin所建议的那样在他的回答中:

  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://sofzh.miximages.com/ruby/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的标题中。 还有其他应该在这里进行本地化或更改的内容吗? 我将屏幕截图更新为英文版。

我知道那个问题,完全忘了! 转到gems目录中的input_elements.rb文件,并以您的语言将文件上载窗口的标题添加到POPUP_TITLES (第443行)。

例:

  • 之前

     POPUP_TITLES = ['Choose file', 'Choose File to Upload'] 
  •  POPUP_TITLES = ['Choose file', 'Choose File to Upload', 'File upload in my language'] 

我现在用英语安装了windows xp,它运行正常! (本地化的Windows Server 2003上发生错误)

我想这是本地化问题。 从现在开始,我将在英文计算机上运行watir。

我今天遇到了同样的问题(2012年3月1日)并通过Google登陆。

感谢Željko指出我正确的方向,但更改[POPUP_TITLES]的解决方案不起作用。 事实上,这个数组似乎不再存在于当前版本的gem(watir-2.0.4)中,或者我只是误读了。

我在watir-2.0.4/lib/watir/dialogs/file_field.rb解决了这个问题:这里,各种窗口和按钮标题被定义为正则表达式。 在以下方法中更改regexp

  • open_button()
  • CANCEL_BUTTON()
  • file_upload_window()

匹配您的本地化窗口名称。 重新加载gem后,它完美无缺。

我建议您查看input_elements.rb中的FileField #set(在您的Ruby gems目录中),并将sleep 1更改为sleep 2 (或更高的数字)。 我注意到在较慢的机器上弹出文件上传需要一秒多的时间。

  @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}')