[英]Testing HTML5 File Upload with Capybara/Selenium Webdriver - Ruby
我有一个简单的模态,其中向用户显示浏览按钮以添加要上传的文件。 由于一个未知的问题,实际上是它的HTML5文件输入,因此浏览器为其添加了自己的功能,这已经成为测试的难题。
在我的页面上,我有:
<input type="file" id="photo_upload">
Capybara提供了一个开箱即用的解决方案:
attach_file <<upload_file_id>>, <<file_path>>
这在后台执行send_keys命令以将file_path推送到用于此输入的路径容器中,但是这对于我的设置根本不起作用。 我在Windows 8上运行Firefox 25.0.1。我尝试了此文件的相对路径和完整路径,并使用正斜杠和反斜杠组合。
当我说它不起作用时,我的意思是当我的ajax脚本通过单击旁边的“上传”按钮执行时,它不会在params中发送任何文件对象。
我什至尝试使用capybara直接发送文件路径:
find_field(<<upload_file_id>>).native.send_keys(<<file_path>>)
接下来,尝试使用硒将硒推入使用:
element = driver.find_element(:id, <<upload_file_id>>)
element.send_keys <<file_path>>
然后,我尝试执行脚本以确保该元素可见,然后进行设置:
element = page.execute_script(
"document.getElementById('#{<<upload_file_id>>}').style.visibility = 'visible';
document.getElementById('#{<<upload_file_id>>}').style.height = '20px';
document.getElementById('#{<<upload_file_id>>}').style.width = '60px';
document.getElementById('#{<<upload_file_id>>}').style.opacity = 1; return
document.getElementById('#{<<upload_file_id>>}')")
find_field(field_locator).native.send_keys(<<file_path>>)
这也不起作用。 现在我完全被困住了。 这里和google上的所有帮助都指向使用上面的方法,但这对我的设置完全不起作用。
据我所知,我的选择是使用Windows自动化脚本并跳出capybara,运行该脚本,然后继续,或者直接使用信息从capybara调用上传URL或调用当前的js ajax可以。
因此,我已经解决了它,并且它不太难看。 我通过AutoIT使用了自动化路线。 您通过AutoIT下载的捆绑软件中包含一个脚本到exe转换器,并使用以下脚本(我不能相信该脚本)创建了一个exe:
Local Const $dialogTitle = $CmdLine[2]
Local Const $timeout = 5
Local $windowFound = WinWait($dialogTitle, "", $timeout)
$windowFound = WinWait($dialogTitle, "", $timeout)
Local $windowHandle
If $windowFound Then
$windowHandle = WinGetHandle("[LAST]")
WinActivate($windowHandle)
ControlSetText($windowHandle, "", "[CLASS:Edit; INSTANCE:1]", $CmdLine[1])
ControlClick($windowHandle, "", "[CLASS:Button; TEXT:&Open]")
Else
MsgBox(0, "", "Could not find window.")
Exit 1
EndIf
在我的水豚脚本中,我只运行:
find_field(<<upload_file_id>>).click
system("<<full_path>>\\file_upload.exe \"#{<<file_path>>}\" \"File Upload\"")
而且效果很好! 实际上,我认为我更喜欢这样的事实,即它完全模仿用户的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.