【发布时间】:2013-12-21 08:28:00
【问题描述】:
我有一个简单的模式出现,其中向用户显示浏览按钮以添加要上传的文件。由于一个未知的问题,它是一个 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 脚本通过单击旁边的“上传”按钮执行时,它不会在参数中发送任何文件对象。
我什至尝试过使用capybara直接发送文件路径:
find_field(<<upload_file_id>>).native.send_keys(<<file_path>>)
接下来,是尝试使用 selenium 来推动它的使用:
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,运行脚本,然后继续,或者使用 post 或调用 js 从 capybara 直接调用上传 url当前执行此操作的 ajax。
【问题讨论】:
标签: ruby-on-rails ruby html selenium-webdriver capybara