Skip to content
This repository has been archived by the owner on Jun 6, 2018. It is now read-only.

Commit

Permalink
implement changes from Jarmo's code review of the dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugh McGowan committed Jun 4, 2011
1 parent f07cdf7 commit 5f2567f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
29 changes: 8 additions & 21 deletions watir/lib/watir/dialogs/file_upload.rb
Expand Up @@ -2,15 +2,18 @@ module Watir
class FileUpload < InputElement
#:stopdoc:
INPUT_TYPES = ["file"]
WINDOW_TITLES = ['Choose file', 'Choose File to Upload']
#:startdoc:

def set(path_to_file)
def set(file_path)
assert_file_exists(file_path)
assert_exists
click_no_wait
set_file_name path_to_file
set_file_name file_path
open_button.click
handle_missing_file path_to_file
end

def assert_file_exists(file_path)
raise WatirException, "#{file_path} has to exist to set!" unless File.exists?(file_path)
end

def set_file_name(path_to_file)
Expand All @@ -25,24 +28,8 @@ def cancel_button
file_upload_window.button(:value => 'Cancel')
end

def handle_missing_file(path_to_file)
#TODO
# window = Watir::Dialog::Window.new(:title => title, :element_title => 'OK', :class => 'Button')
# if window.exists?
# window.button('OK').click
# raise "File not found: #{path_to_file}"
# end
# cancel_button.click
end

def file_upload_window
unless @window
Watir::Wait.until {
@window = ::RAutomation::Window.new(:title => /^(#{WINDOW_TITLES.join('|')})$/)
@window.exists?
}
end
@window
@window ||= RAutomation::Window.new(:title => /^choose file( to upload)?$/i)
end

end
Expand Down
11 changes: 6 additions & 5 deletions watir/lib/watir/dialogs/javascript.rb
Expand Up @@ -28,11 +28,12 @@ def text

def javascript_dialog_window
return @window if @window
RAutomation::Window.wait_timeout = @timeout
if @opts[:title]
@window = ::RAutomation::Window.new(:title => @opts[:title])
else
@window = ::RAutomation::Window.new(:title => /^(#{WINDOW_TITLES.join('|')})$/)
begin
original_timeout = RAutomation::Window.wait_timeout
RAutomation::Window.wait_timeout = @timeout
::RAutomation::Window.new(:title => @opts[:title] || /^(#{WINDOW_TITLES.join('|')})$/)
ensure
RAutomation::Window.wait_timeout = original_timeout
end
@window
end
Expand Down
5 changes: 4 additions & 1 deletion watir/unittests/filefield_xpath_test.rb
Expand Up @@ -18,7 +18,10 @@ def test_file_field_Exists
assert_false(browser.file_field(:xpath, "//input[@name='missing']/").exists?)
assert_false(browser.file_field(:xpath, "//input[@name='totallybogus']/").exists?)
#pop one open and put something in it.
browser.file_field(:xpath, "//input[@name='file1']/").set($htmlRoot + "fileupload.html")
file = $htmlRoot + "fileupload.html"
file.gsub! 'file:///', ''
file.gsub! '/', '\\'
browser.file_field(:xpath, "//input[@name='file1']/").set(file)
#click the upload button
browser.button(:xpath, "//input[@name='upload']/").click

Expand Down

0 comments on commit 5f2567f

Please sign in to comment.