ruby-on-rails-3 - 表单提交后 Capybara 不传递 header

标签 ruby-on-rails-3 header cucumber jquery-mobile capybara

我正在构建一个 Rails 3 应用程序,它为移动设备(使用 jQuery Mobile)和常规桌面浏览器呈现不同的 View 。我正在使用 Cucumber/Capybara 进行测试,并且针对移动设备和浏览器有单独的测试套件。我可以使用此处找到的“headers hack”在请求的 HTTP header 中设置移动用户代理字符串...

Using Cucumber/Capybara in Rails 3, how do I set a custom User-Agent string?

问题...

我的大多数移动 Cucumber 步骤都工作正常(例如,我在步骤中设置移动标题,然后渲染移动 View )。但是,在提交表单后,测试将呈现下一步的浏览器 View (不是我想要的移动 View )。我认为这可能是因为 Capybara 在表单提交操作后删除了标题。

我尝试通过向 Controller 操作添加记录器来测试这一点,以便我可以查看 user_agent header ,如下所示:

def show
    # …
    logger.info("Headers: #{request.user_agent}")
    # …
end

我使用了 iPhone header (买家是资源)。在我的 test.log 中我看到:

对于创建操作...

Started POST "/buyers" for 127.0.0.1 at 2011-04-19 16:49:18 -0700
Processing by BuyersController#create as HTML
  #...
Headers: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
  #...
Redirected to http://www.example.com/buyers/1
Completed 302 Found in 7ms

对于后续的显示操作(注意“标题:”为空)...

Started GET "/buyers/1" for 127.0.0.1 at 2011-04-19 16:49:18 -0700
Processing by BuyersController#show as HTML
 #...
Headers: 
 #...
Completed 200 OK in 4ms (Views: 2.8ms | ActiveRecord: 1.4ms)

正如人们所料,“显示页面”步骤随后会呈现浏览器页面,而不是我想要的移动页面。

如何保留 header 以便我的所有移动步骤都在我的移动测试套件中运行?

更新

Jnicklas(真正令人敬畏的 capybara gem 的创造者!)回应:

“说实话,这与其说是一个问题,不如说是一个问题。据我所知,在 Rack-Test 中,目前还没有办法实现如此短的猴子补丁。我一直在考虑向机架测试驱动程序添加一个 header 选项,这将允许传入不同的 header 选项。如果您使用大量 JavaScript,Selenium 可能更适合,并且允许您自由设置用户代理,IIRC。”

https://github.com/jnicklas

https://github.com/jnicklas/capybara

最佳答案

此功能已于 2011 年 4 月 25 日通过此提交添加到 Capybara - https://github.com/jnicklas/capybara/commit/a00435d63085ac2e74a0f64e7b7dedc0f7252ea9

您现在可以在使用自定义 Capybara 驱动程序时指定自定义 header 。请参阅http://automagical.posterous.com/creating-a-custom-capybara-driver代码示例。

关于ruby-on-rails-3 - 表单提交后 Capybara 不传递 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5735065/

相关文章:

ruby-on-rails - Ruby - 如何解析数组中的日期

ruby - 如何从s3读取文件?

cucumber - 在 Cucumber 场景中将空格作为参数表的值

mysql - 为什么我收到 "Could not find table ' users'"错误?

ruby-on-rails - 运行时出现段错误 'rails s'

c++ - 使用更多标准类型显式实例化给定模板函数的函数?

c - C 和/或 Objective-C 中的组头文件

html - "head"和 "header"标签之间的真正区别是什么?

ruby - 使用试运行选项运行 cucumber 时出错

java - 从 info.cukes 迁移到 io.cucumber 在运行测试时不显示任何步骤状态