如何使用watir和IE上传文件? [英] How to upload a file with watir and IE?

查看:131
本文介绍了如何使用watir和IE上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

这是我的watir脚本:

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

我从这个页面得到了代码: http://wiki.openqa.org/display/WTR/File+Uploads

I got the code from this page: 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进行测试。

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.

编辑:我在这里上传了我的简单示例(3个文件存在于c中:\testing \在我的机器上,只需启动cmd文件):

I have uploaded my simple example here (3 files that live in c:\testing\ on my machines, just start the cmd file):

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中改变了睡眠时间第二到第5秒,就像ŽeljkoFilipin在回答中所建议的那样:

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:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.5\lib\watir\input_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 '
          ...

这是停止的地方(请注意我手动导航到文件对话框中的目录一次。从那时起,IE总是显示该目录的打开对话框,但这并不意味着脚本选择了目录。我认为这意味着IE总是显示最后一个目录它离开了):

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):

这是它停止的地方http://dl.dropbox.com/u/1508092/upload-dialog.JPG

编辑:

我发现ole32代码查找英文标题:

I found that that the ole32 code looks for the english title:

POPUP_TITLES = ['选择文件','选择要上传的文件']

POPUP_TITLES = ['Choose file', 'Choose File to Upload']

我现在安装了IE 7英文版。仍然没有成功。但我认为它与本地化有关,因为input_elements.rb搜索窗口标题。我想知道为什么它现在仍然失败。这是来自input_elements.rb的代码:

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

文本选择文件现在出现在我的新IE的标题中。还有其他应该在这里进行本地化或更改的内容吗?我将截图更新为英文版。

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.

推荐答案

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

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).

示例:


  • 之前

  • before

POPUP_TITLES = ['Choose file', 'Choose File to Upload']


  • after

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


  • 这篇关于如何使用watir和IE上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆