Ruby/Cucumber/Capybara 测试分段文件上传
我正在使用 Cucumber/Capybara 来测试 Web 应用程序。我几乎是 Ruby 的初学者,这对 Cucumber/Capybara 的开发人员来说是一个真正的见证,证明我仅用我所拥有的极少的 Ruby 知识就能够测试我的应用程序。
然而,正如您可能已经猜到的那样,我已经到了需要专家帮助的地步了。我需要测试分段文件上传。问题是我正在测试的 Web 应用程序有一个 URL 命令界面,但没有关联的页面。所以我不能只加载页面、填写参数并按下按钮。我必须以编程方式格式化 POST 命令。
到目前为止,我一直仅使用“访问”与该应用程序进行交互。即我有步骤定义,例如:
Given /^I delete an alert with alertID "([^"]*)" from the site$/ do |alertID|
visit WEB_SITE_ROOT + "/RemoteService?command=deleteAlert&siteName=#{$Site}&alertID=#{alertID}"
end
但现在我需要做一些帖子。我发现一些代码似乎可以满足我的需要:
Given /^I upload the "([^"]*)" file "([^"]*)" for the alert$/ do |fileType, fileName|
file = File.new(fileName, "rb")
reply = RestClient.post(
"#{WEB_SITE_ROOT}" + "/FileUploader?command=upload&siteName=#{$Site}&alertID=#{$OriginalAlertID}",
:pict => file,
:function => "#{fileType}",
:content_type => 'multipart/jpg',
)
end
但这不是在同一个黄瓜/水豚会话中运行,因此未获得授权(前面的步骤之一是登录)。另外,cucumber/capybara 不会接收来自网络应用程序的回复,因此我的成功/失败测试不起作用。
有人可以指出我正确的方向吗?
I'm using Cucumber/Capybara to test a web application. I'm pretty much a complete beginner in Ruby and its a real testimony to the developers of Cucumber/Capybara just how far I have been able to test my application with only the miniscule amount of Ruby knowledge that I have.
However, as you've probably guessed, I've reach the point were I need some expert help. I need to test a multipart file upload. The problem is that the web application that I'm testing has a URL command interface, but no associated pages. So I can't just load the page, fill in a parameter and push a button. I have to format the POST command programatically.
Up until now, I have been interacting this the application exclusively using 'visit'. i.e. i have steps definitions such as:
Given /^I delete an alert with alertID "([^"]*)" from the site$/ do |alertID|
visit WEB_SITE_ROOT + "/RemoteService?command=deleteAlert&siteName=#{$Site}&alertID=#{alertID}"
end
But now I need to do some posts. I found some code that seems to do what I need:
Given /^I upload the "([^"]*)" file "([^"]*)" for the alert$/ do |fileType, fileName|
file = File.new(fileName, "rb")
reply = RestClient.post(
"#{WEB_SITE_ROOT}" + "/FileUploader?command=upload&siteName=#{$Site}&alertID=#{$OriginalAlertID}",
:pict => file,
:function => "#{fileType}",
:content_type => 'multipart/jpg',
)
end
But this is not running in the same cucumber/capybara session, and so is not authorised (one of the previous steps was a login). Also, the reply from the web application is not picked up by cucumber/capybara and so my test for success/failure do not work.
Can someone please point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,水豚使用 Rack::Test 适配器,它将绕过 HTTP 服务器并直接与您的 Rack/您的应用程序交互。您在步骤中执行的 POST 请求不会通过水豚,因此它失败了。
要在使用
Rack::Test
时上传文件,您需要使用 Rails #fixture_file_upload 方法,默认情况下应该在您的黄瓜步骤中可用。By default capybara uses the
Rack::Test
adapter which will bypass the HTTP server and interact with your Rack/your app directly. The POST request you're doing in your step won't go through capybara, hence why it's failing.To upload files when using
Rack::Test
you'll need to use the Rails #fixture_file_upload method, which by default should be available in your cucumber steps.