How to upload a file with watir and IE?

前端 未结 5 1352
情歌与酒
情歌与酒 2020-12-18 01:07

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.

Instead I

相关标签:
5条回答
  • 2020-12-18 01:44

    I had the same problem today (March1,2012) and landed here via Google.

    Thanks Željko for pointing me in the right direction, however the solution of changing the [POPUP_TITLES] didn't work. In fact, this array seems not to exist anymore in the current version of the gem (watir-2.0.4), or maybe I just misread.

    I solved the problem in watir-2.0.4/lib/watir/dialogs/file_field.rb: Here, the various window and button titles are defined as regular expressions. Change the regexps in the following methods

    • open_button()
    • cancel_button()
    • file_upload_window()

    to match your localized window names. After reloading the gem, it worked flawlessly.

    0 讨论(0)
  • 2020-12-18 01:50

    I installed windows xp in english now, and it works! (The error occured on a localised windows server 2003)

    I guess it was the localisation issue. I will just run watir on the english computer from now on.

    0 讨论(0)
  • 2020-12-18 02:00

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

    Example:

    • before

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

      POPUP_TITLES = ['Choose file', 'Choose File to Upload', 'File upload in my language']
      
    0 讨论(0)
  • 2020-12-18 02:01

    I would suggest that you take a look at FileField#set in input_elements.rb (in your Ruby gems directory), and change sleep 1 to sleep 2 (or some higher number). I have noticed that on slower machines it takes more than a second for file upload pop up to appear.

    0 讨论(0)
  • 2020-12-18 02:06
      @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}')
    
    0 讨论(0)
提交回复
热议问题