use*_*021 7 ruby internet-explorer watir
我正在编写一个watir脚本来测试上传表单.
但是脚本不会自动选择要从我的硬盘上传的文件.
相反,IE会在文件选择器对话框打开时停止.只要我在对话框中手动选择要上传的文件并单击"确定",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
Run Code Online (Sandbox Code Playgroud)
我从这个页面得到了代码: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>
Run Code Online (Sandbox Code Playgroud)
我也找到了这本手册http://svn.openqa.org/svn/watir/trunk/watir/unittests/filefield_test.rb.我正在使用IE 6和IE 7进行测试.
编辑:我在这里上传了我的简单示例(3个文件位于我的机器上的c:\ testing \中,只需启动cmd文件):
http://dl.dropbox.com/u/1508092/testing.rar
它在3台不同的机器上失败(所有Windows 2003,2x IE 6和1 x IE 7).我还在脚本c:\ ruby\lib\ruby\gems\1.8\gems\watir-1.6.5\lib\watir\input_elements.rb中将睡眠时间从1秒改为5秒,如ŽeljkoFilipin所建议的那样在他的回答中:
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 '
...
Run Code Online (Sandbox Code Playgroud)
这是它停止的地方(请注意我手动导航到文件对话框中的目录一次.从那时起,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
Run Code Online (Sandbox Code Playgroud)
"选择文件"文本现在出现在我的新IE的标题中.还有其他什么应该在这里进行本地化或更改?我将屏幕截图更新为英文版.
我知道这个问题,但完全忘记了!转到gems 目录中的input_elements.rb文件,并将您的语言的文件上传窗口的标题添加到POPUP_TITLES(第 443 行)。
例子:
前
POPUP_TITLES = ['Choose file', 'Choose File to Upload']
Run Code Online (Sandbox Code Playgroud)后
POPUP_TITLES = ['Choose file', 'Choose File to Upload', 'File upload in my language']
Run Code Online (Sandbox Code Playgroud)