アップロード フォームをテストするための watir スクリプトを作成しています。
しかし、スクリプトはハードドライブからアップロードするファイルを自動的に選択しません。
代わりに、IE はファイル選択ダイアログを開いたまま停止します。ダイアログでアップロードするファイルを手動で選択し、[OK] をクリックするとすぐに、watir は必要に応じて続行します。なんで止まるのかしら。
これは私のwatirスクリプトです:
require 'test/unit'
require 'watir'
# runs on win3k, IE 6.0.3790; ruby 1.8.6, watir
class EpcHomePage < Test::Unit::TestCase
def test_upload
ie = @browser
htmlfile = "C:\\testing\\upload.html"
uploadfile = "C:\\testing\\upload.html"
ie.goto(htmlfile)
ie.file_field(:name,"file1").set(uploadfile)
assert_equal uploadfile, ie.file_field(:name,"file1").value
ie.button(:name, 'upload').click
end
def setup
@browser = Watir::IE.new
end
def teardown
@browser.close
end
end
このページからコードを取得しました: http://wiki.openqa.org/display/WTR/File+Uploads
これは次の形式です。
<html><body>
<form name="form1" enctype="multipart/form-data" method="post" action="upload.html">
<input type="file" name="file1">
<input type="submit" name="upload" value="ok">
</form>
</body></html>
このマニュアルhttp://svn.openqa.org/svn/watir/trunk/watir/unittests/filefield_test.rbも見つけました。テストには IE 6 と IE 7 を使用しています。
編集:ここに簡単な例をアップロードしました (私のマシンの c:\testing\ にある 3 つのファイル、cmd ファイルを開始するだけです):
http://dl.dropbox.com/u/1508092/testing.rar
3 つの異なるマシン (すべての Windows 2003、2x IE 6 および 1x IE 7) で失敗します。また、スクリプト c:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.5\lib\watir\input_elements.rb のスリープ時間を 1 秒から 5 秒に変更しました。彼の答えで:
def set(path_to_file)
assert_exists
require 'watir/windowhelper'
WindowHelper.check_autoit_installed
begin
Thread.new do
sleep 5 # it takes some time for popup to appear
system %{ruby -e '
...
ここで停止します (一度、ファイル ダイアログのディレクトリに手動で移動したことに注意してください。その時点から、IE は常にこのディレクトリで開いているダイアログを表示しますが、スクリプトがディレクトリを選択したことを意味するわけではありません。これは、IE が常に最後のディレクトリを表示することを意味します):
ここで停止します http://dl.dropbox.com/u/1508092/upload-dialog.JPG
編集:
ole32 コードが英語のタイトルを探していることがわかりました。
POPUP_TITLES = ['ファイルを選択', 'アップロードするファイルを選択']
IE 7英語版をインストールしました。まだ成功していません。しかし、input_elements.rb はウィンドウ タイトルを検索するため、ローカライズと関係があると思います。なぜ今でも失敗するのだろうか。これはinput_elements.rbのコードです:
class FileField < InputElement
INPUT_TYPES = ["file"]
POPUP_TITLES = ['Choose file', 'Choose File to Upload']
# set the file location in the Choose file dialog in a new process
# will raise a Watir Exception if AutoIt is not correctly installed
def set(path_to_file)
assert_exists
require 'watir/windowhelper'
WindowHelper.check_autoit_installed
begin
Thread.new do
sleep 2 # 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
end
新しい IE のタイトルに「ファイルを選択してください」というテキストが表示されるようになりました。ここでローカライズまたは変更する必要があるものは他にありますか? スクリーンショットを英語版に更新しました。