如何在 Capybara 中 POST 到 URL? [英] How do you POST to a URL in Capybara?

查看:35
本文介绍了如何在 Capybara 中 POST 到 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚从 Cucumber+Webrat 切换到 Cucumber+Capybara,我想知道如何将内容发布到 Capybara 中的 URL.

Just switched from Cucumber+Webrat to Cucumber+Capybara and I am wondering how you can POST content to a URL in Capybara.

在 Cucumber+Webrat 中,我能够有一个步骤:

In Cucumber+Webrat I was able to have a step:

When /^I send "([^"]*)" to "([^"]*)"$/ do |file, project|
  proj = Project.find(:first, :conditions => "name='#{project}'")
  f = File.new(File.join(::Rails.root.to_s, file))
  visit "project/" + proj.id.to_s + "/upload",
        :post, {:upload_path => File.join(::Rails.root.to_s, file)}
end

但是,Capybara 文档提到:

However, the Capybara documentation mentions:

visit方法只需要一个参数,请求方法是总是 GET.总是 GET.

The visit method only takes a single parameter, the request method is always GET.always GET.

如何修改我的步骤,以便 Cucumber+Capybara 对 URL 进行 POST?

How do I modify my step so that Cucumber+Capybara does a POST to the URL?

推荐答案

最近我发现了这篇很棒的博文.这对于像托尼这样的案例以及你真正想要在你的库克中发布一些东西来说非常有用:

More recently I found this great blog post. Which is great for the cases like Tony and where you really want to post something in your cuke:

就我而言,这变成了:

def send_log(file, project)
  proj = Project.find(:first, :conditions => "name='#{project}'")
  f = File.new(File.join(::Rails.root.to_s, file))
  page.driver.post("projects/" + proj.id.to_s + "/log?upload_path=" + f.to_path)
  page.driver.status_code.should eql 200
end

这篇关于如何在 Capybara 中 POST 到 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆