views:

232

answers:

2

How can I choose a file from a file browse dialog (i.e., uploading a file from my PC) in a Ruby Selenium automation script?

+1  A: 

I think I've run into this problem before.

When you write selenium scripts in Ruby, you can control all the windows of the browser. But the file chooser dialog, and the file download dialog, are actually system windows, so you can't control them via selenium.

You can, however, control them via the Win32OLE gem, for tests running on Windows. But of course then you can't run those tests on Macs or Linux.

Like selenium in general, it's kind of hacky. But here's how it works:

 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
Sarah Mei
A: 

I would skip all the OLE stuff and just type the path in to the field :)

You can do this as long as you're running Selenium RC with one of the privileged modes. Those are used by default if you're on the latest 1.0 beta 2.

Patrick Lightbody