【问题标题】:How to upload a file with watir and Internet Explorer?如何使用 watir 和 Internet Explorer 上传文件?
【发布时间】:2011-11-17 01:15:00
【问题描述】:

在过去的几天里,我负责自动化我公司 CMS 系统的某些方面。 (我们正在使用 Drupal)

我的 ruby​​ 脚本遇到的一个问题是让 IE 从我的机器中选择一个本地文件并将其添加到我的提交表单中。

现在我一直在阅读这方面的内容并找到了一些类似的帖子,但与我们使用的表单不​​同的是,浏览按钮是由 Flash 制成的,并且没有文件路径的输入字段。我的选项是输入“文件名”并单击“打开”按钮,或者第二个选项是双击文件。

所以到目前为止我解决这个问题的方法是向下切换到“浏览器按钮,然后按“Enter”键打开浏览菜单。但我现在需要选择文件名的能力。

这是我的 Ruby 文件。

require 'watir'
include Watir

ie = Watir::IE.new
ie.goto ("file:///C:/Ruby193/bin/ruby-capybara/features/step_definitions/form.html")

ie.text_field(:id, "edit-submitted-ugc-video-title").set("Title")
ie.text_field(:id, "edit-submitted-ugc-video-firstname").set("First Name")
ie.text_field(:id, "edit-submitted-ugc-video-lastname").set("Last Name")
ie.text_field(:id, "edit-submitted-ugc-video-phonenumber").set("01234567891")
ie.text_field(:id, "edit-submitted-ugc-video-location").set("London")
ie.text_field(:id, "edit-submitted-ugc-video-email").set("test@test.com")
ie.text_field(:id, "edit-submitted-ugc-video-email2").set("test@test.com")
ie.send_keys('{TAB}')
ie.send_keys('{ENTER}')

uploadfile = "V:\GIR-FP-WSD-QA\Test Media Files\Video1.mp4"
assert_equal uploadfile, ie.file_field(:name,"Video1.mp4").value    
ie.button(:name, 'upload').click
#

编辑:

需要“女仆” 需要'win32ole'

包括瓦提尔

ie = Watir::IE.new
ie.goto ("file:///C:/Ruby193/bin/ruby-capybara/features/step_definitions/form.html")

ie.text_field(:id, "edit-submitted-ugc-video-title").set("Title")
ie.text_field(:id, "edit-submitted-ugc-video-firstname").set("First Name")
ie.text_field(:id, "edit-submitted-ugc-video-lastname").set("Last Name")
ie.text_field(:id, "edit-submitted-ugc-video-phonenumber").set("01234567891")
ie.text_field(:id, "edit-submitted-ugc-video-location").set("London")
ie.text_field(:id, "edit-submitted-ugc-video-email").set("test@test.com")
ie.text_field(:id, "edit-submitted-ugc-video-email2").set("test@test.com")
ie.send_keys('{TAB}')
ie.send_keys('{ENTER}')

class FileField < InputElement

# 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(setPath)
assert_exists
require 'watir/windowhelper'
WindowHelper.check_autoit_installed
begin
    thrd = Thread.new do
    system("rubyw -e \"require 'win32ole'; @autoit=WIN32OLE.new('AutoItX3.Control');
    waitresult=@autoit.WinWait 'Bird annoys Cat.mp4', '', 15; sleep 1; if waitresult == 1\" -e 
    \"@autoit.ControlSetText 'Bird annoys Cat.mp4', '', 'Edit1', '#{setPath}'; 
    @autoit.ControlSend 'Bird annoys Cat.mp4', '', 'Button2', '{ENTER}';\" -e \"end\"")
end
    thrd.join(1)
rescue
    raise Watir::Exception::WatirException, "Problem accessing Choose file dialog"
end
click
$end

ie.button(:name, 'upload').click

【问题讨论】:

  • 请发布相关的HTML,或链接到该页面,或链接到类似的页面。
  • 不要'包含 watir' 要求它就是你所需要的。

标签: ruby flash internet-explorer file-upload watir


【解决方案1】:

恐怕在这种情况下您必须使用 Windows 自动化。 它可能是winapi或某种自动化框架,例如autoit

  class FileField < InputElement

# 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(setPath)
  assert_exists
  require 'watir/windowhelper'
  WindowHelper.check_autoit_installed
  begin
    thrd = Thread.new do
      system("rubyw -e \"require 'win32ole'; @autoit=WIN32OLE.new('AutoItX3.Control');
      waitresult=@autoit.WinWait 'Choose file', '', 15; sleep 1; if waitresult == 1\" -e 
      \"@autoit.ControlSetText 'Choose file', '', 'Edit1', '#{setPath}'; 
      @autoit.ControlSend 'Choose file', '', 'Button2', '{ENTER}';\" -e \"end\"")
    end
  thrd.join(1)
  rescue
    raise Watir::Exception::WatirException, "Problem accessing Choose file dialog"
  end
  click
end

结束

【讨论】:

  • 感谢您的回复,我已尝试运行以下命令,但我似乎仍然获得与以前相同的体验。但是我从未使用过 Autoit,所以我可能做错了什么。
【解决方案2】:

由于它的专有性,在 Flash 中完成的工作有时会非常痛苦。您可能想研究在这部分测试中使用基于图像的自动化方法。请参阅this blog posting 了解更多信息

【讨论】:

    【解决方案3】:

    文件选择对话框是原生的 Windows 文件打开对话框还是专有的闪存解决方案?

    如果它是本机对话框(例如,与其他任何地方相同),那么您可以使用我的库 RAutomation 或 autoit,如上所述。

    如果是这种情况,那么您可以从Watir's code 获得一些提示。

    另外,Flash 上传是否有一些 JavaScript API?如果是这种情况,那么您可以使用 #execute_script 来做您需要的所有事情 - 例如。以编程方式设置文件并继续。我正在使用 Flash 视频播放器做类似的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-10
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 2012-12-18
      相关资源
      最近更新 更多