Class: Vapir::IE::FileField

Inherits:
InputElement show all
Includes:
FileField
Defined in:
lib/vapir-ie/input_elements.rb

Overview

For fields that accept file uploads Windows dialog is opened and handled by WinWindow (see scripts/select_file.rb), launched in a new process.

Instance Method Summary collapse

Methods inherited from Element

#after?, #assert_enabled, #before?, #click, #click_no_wait, element_object_style, #enabled?, #fire_event, #fire_event_no_wait, #scroll_offset, #text_after_begin, #text_after_end, #text_before_begin, #text_before_end, #wait

Methods included from Container

#handling_existence_failure, #log

Instance Method Details

#set(file_path) ⇒ Object

set the file location in the Choose file dialog



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/vapir-ie/input_elements.rb', line 104

def set(file_path)
  assert_exists do
    # it would be nice to do a click_no_wait and then enter the select file dialog information
    # in the same thread, but that won't work here, because #click_no_wait runs in a javascript 
    # setTimeout, and javascript calling to a file input's .click() method doesn't work. it appears 
    # to work until you submit the form, at which point it says access is denied. so, the click has 
    # to be done via WIN32OLE, but it doesn't return until the file field is set, so we need two
    # ruby processes (since WIN32OLE blocking blocks all ruby threads).
    # since locating this element is a much more complicated task than locating the enabled_popup
    # of this browser, this process will handle the former, and we'll spawn another to do the
    # latter.
    require 'win32/process'
    Vapir.require_winwindow
    rubyw_exe= File.join(Config::CONFIG['bindir'], 'rubyw')
    error_file_name=File.expand_path(File.join(File.dirname(__FILE__), 'scripts', 'select_file_error_status.marshal_dump'))
    select_file_script=File.expand_path(File.join(File.dirname(__FILE__), 'scripts', 'select_file.rb'))
    command_line=rubyw_exe+[select_file_script, browser.hwnd.to_s, file_path, error_file_name].map{|arg| " \"#{arg.gsub("/", "\\")}\""}.join('')
     # TODO/FIX: the above method of escaping seems to have issues with trailing slashes. 
    select_file_process=::Process.create('command_line' => command_line)
    
    click
    
    if ::Waiter.try_for(2, :exception => nil) { File.exists?(error_file_name) } # wait around a moment for the script to finish writing - #click returns before that script exits 
      marshaled_error=File.read(error_file_name)
      error=Marshal.load(marshaled_error)
      error[:backtrace]+= caller(0)
      File.delete(error_file_name)
      raise error[:class], error[:message], error[:backtrace]
    end
    return file_path
    # TODO/FIX: figure out what events ought to be fired here - onchange, maybe others
  end
end