Class: Watir::FileField

Inherits:
InputElement show all
Defined in:
lib/watir/input_elements.rb

Overview

For fields that accept file uploads Windows dialog is opened and handled in this case by autoit launching into a new process. Normally a user would not need to create this object as it is returned by the Watir::Container#file_field method

Constant Summary collapse

INPUT_TYPES =

:stopdoc:

["file"]
['Choose file', 'Choose File to Upload']

Constants inherited from Element

Element::TO_S_SIZE

Instance Attribute Summary

Attributes inherited from Element

#container

Attributes included from Container

#activeObjectHighLightColor, #page_container, #type_keys, #typingspeed

Instance Method Summary collapse

Methods inherited from InputElement

#initialize, #locate

Methods inherited from Element

#<=>, #activeObjectHighLightColor, #after_text, #assert_enabled, #assert_exists, #attribute_value, #before_text, #click, #click!, #click_no_wait, #document, #enabled?, #exists?, #fire_event, #flash, #focus, #initialize, #inspect, #ole_object, #ole_object=, #parent, #text, #to_s, #type_keys, #typingspeed, #visible?

Methods included from Container

#area, #areas, #button, #buttons, #cell, #cells, #checkbox, #checkboxes, #dds, #divs, #dls, #dts, #element, #elements, #ems, #file_field, #file_fields, #form, #forms, #frame, #hidden, #hiddens, #image, #images, #labels, #link, #links, #lis, #locate_all_elements, #locate_input_element, #locate_tagged_element, #log, #map, #maps, #modal_dialog, #popup, #pres, #ps, #radio, #radios, #row, #rows, #select_list, #select_lists, #set_container, #show_all_objects, #spans, #strongs, #table, #tables, #text_field, #text_fields, #wait

Constructor Details

This class inherits a constructor from Watir::InputElement

Instance Method Details

#set(path_to_file) ⇒ Object

set the file location in the Choose file dialog in a new process will raise a WatirException if AutoIt is not correctly installed



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/watir/input_elements.rb', line 492

def set(path_to_file)
  assert_exists
  require 'watir/windowhelper'
  WindowHelper.check_autoit_installed
  begin
    Thread.new do
      sleep 1 # it takes some time for popup to appear

      system %{ruby -e '
          require "win32ole"

          @autoit = WIN32OLE.new("AutoItX3.Control")
          time    = Time.now

          while (Time.now - time) < 15 # the loop will wait up to 15 seconds for popup to appear
            #{POPUP_TITLES.inspect}.each do |popup_title|
              next unless @autoit.WinWait(popup_title, "", 1) == 1

              @autoit.ControlSetText(popup_title, "", "Edit1", #{path_to_file.inspect})
              @autoit.ControlSend(popup_title, "", "Button2", "{ENTER}")
              exit
            end # each
          end # while
      '}
    end.join(1)
  rescue
    raise Watir::Exception::WatirException, "Problem accessing Choose file dialog"
  end
  click
end