浏览字段

时间:2009-04-01 12:27:53

标签: ruby selenium win32ole

如何从Ruby Selenium自动化脚本中选择文件浏览对话框中的文件(即从我的PC上传文件)?

2 个答案:

答案 0 :(得分:2)

我想我以前遇到过这个问题。

当您在Ruby中编写selenium脚本时,您可以控制浏览器的所有窗口。但文件选择器对话框和文件下载对话框实际上是系统窗口,因此您无法通过selenium控制它们。

但是,对于在Windows上运行的测试,您可以通过Win32OLE gem控制它们。但当然,你无法在Mac或Linux上运行这些测试。

就像硒一般,它有点像hacky。但这是它的工作原理:

 require 'selenium'
 require 'test/unit'
 require 'win32ole'

 class DownloadFileTest < Test::Unit::TestCase
   def setup()
     @wsh = WIN32OLE.new('Wscript.Shell')
   end
   def teardown
     WIN32OLE.ole_free(@wsh) # yes, this is required *rolls eyes*
   end
   def test_download_file
     # ...stuff that causes a download window to pop up...
     @wsh.AppActivate("Opening")
     sleep(2)
     @wsh.SendKeys("{RIGHT}{ENTER}") # Hits ok button - file downloads
     sleep(3)
     # Use regular Ruby File methods to assert stuff on the file content
   end

答案 1 :(得分:0)

我会跳过所有OLE的东西,只需输入到该字段的路径:)

只要您使用其中一种特权模式运行Selenium RC,就可以执行此操作。如果您使用的是最新的1.0 beta 2,则默认使用这些。

相关问题