在Rails 3中使用Cucumber / Capybara,如何设置自定义User-Agent字符串? [英] Using Cucumber/Capybara in Rails 3, how do I set a custom User-Agent string?

查看:148
本文介绍了在Rails 3中使用Cucumber / Capybara,如何设置自定义User-Agent字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails 3应用程序,我有不同的布局为iPhone和桌面浏览器。我试图测试iPhone布局使用黄瓜/水豚。到目前为止,我在请求的HTTP头中设置iPhone User-Agent字符串的所有尝试都失败了。



我已经按照用Cucumber和Capybara测试自定义标头和ssl 教程,但它不' t似乎在HTTP请求中设置User-Agent字符串。



如果我使用我的iPhone浏览到我的Rails应用程序,我得到正确的布局。我使用 Rack-Mobile-Detect 将Rails request.format设置为:iphone 。



有关如何使这项工作的任何想法?



这里是我到目前为止:

 功能:检测浏览器
为了使iPhone和桌面浏览器具有不同的布局
作为开发人员
我想为不同的浏览器显示不同的布局

场景:使用桌面布局显示主页
考虑到我使用的是桌面浏览器
当我转到主页
然后我应该看到桌面浏览器

场景:使用iPhone布局显示主页
考虑到我使用mobile safari
当我去主页
显示页面
然后我应该看到mobile safari

Detect_browser_steps.rb



 给定/ ^(?: | I) 
case browser
mobile safari
agent =Mozilla / 5.0(iPhone; U; CPU iPhone OS 3_1_2如Mac OS X; en-us)AppleWebKit / 528.18 Gecko)Version / 4.0 Mobile / 7D11 Safari / 528.16
add_headers({'User-Agent'=> agent})
else
#不设置特殊的用户代理
end
end

headers_hack.rb

 #http://aflatter.de/2010/06/testing-headers-and-ssl-with-cucumber-and-capybara/ 
#以下解决方案仅在您使用:rack_test驱动程序时才有效。
module RackTestMixin

def self.included(mod)
mod.class_eval do
#这是我们保存额外条目的地方。
def hacked_env
@hacked_env || = {}
end

#别名原始方法以供进一步使用。
alias_method:original_env,:env

#覆盖该方法以合并附加头。
#加上这隐含地使它公开。
def env
original_env.merge(hacked_env)
end
end
end

end

Capybara: :Driver :: RackTest.send:include,RackTestMixin

模块HeadersHackHelper

def add_headers(headers)
page.driver.hacked_env.merge! b $ b end

end

世界(HeadersHackHelper)


解决方案

不得不在这一点上弄错了,但最终我设法让它工作只是通过添加:

 #features / support / capybara_headers.rb:

模块CapybaraHeadersHelper
def add_headers(headers)
headers.each do | name,value |
page.driver.browser.header(name,value)
end
end
end
世界(CapybaraHeadersHelper)



您可以在此

In my Rails 3 app, I have different layouts for iPhone vs desktop browsers. I'm trying to test the iPhone layout using Cucumber/Capybara. So far, all my attempts at setting an iPhone User-Agent string in the request's HTTP header have failed.

I have followed the Testing custom headers and ssl with Cucumber and Capybara tutorial but it doesn't seem to set the User-Agent string in the HTTP request.

If I just browse to my Rails app using my iPhone, I get the right layout. I am using Rack-Mobile-Detect to set the Rails request.format to :iphone.

Any ideas on how to make this work? I'm about ready to ditch Capybara and go back to Webrat.

Here's what I have so far:

Feature: Detect Browser
  In order to have different layouts for iPhone vs. desktop browsers
  As a developer
  I want to show different layouts for different browsers

Scenario: Show home page with desktop layout 
  Given I am using "a desktop browser"
  When I go to "the home page"
  Then I should see "desktop browser"

Scenario: Show home page with iPhone layout
  Given I am using "mobile safari"
  When I go to "the home page"
  Then show me the page
  Then I should see "mobile safari"

Detect_browser_steps.rb

Given /^(?:|I )am using (.+)$/ do |browser|
  case browser
  when "mobile safari"
    agent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16"
    add_headers({'User-Agent' => agent})
  else
    # don't set a special User-Agent header
  end
end

headers_hack.rb

# http://aflatter.de/2010/06/testing-headers-and-ssl-with-cucumber-and-capybara/
# The following solution will work only if you use the :rack_test driver.
module RackTestMixin

  def self.included(mod)
    mod.class_eval do
      # This is where we save additional entries.
      def hacked_env
        @hacked_env ||= {}
      end

      # Alias the original method for further use.
      alias_method  :original_env, :env

      # Override the method to merge additional headers.
      # Plus this implicitly makes it public.
      def env
        original_env.merge(hacked_env)
      end
    end
  end

end

Capybara::Driver::RackTest.send :include, RackTestMixin

module HeadersHackHelper

  def add_headers(headers)
    page.driver.hacked_env.merge!(headers)
  end

end

World(HeadersHackHelper)

解决方案

Had to fiddle a bit on this, but in the end I managed to get it working just by adding:

# features/support/capybara_headers.rb:

module CapybaraHeadersHelper
  def add_headers(headers)
    headers.each do |name, value|
      page.driver.browser.header(name, value)
    end
  end
end
World(CapybaraHeadersHelper)

You can get more info in this blog post I wrote, but that's basically it.

这篇关于在Rails 3中使用Cucumber / Capybara,如何设置自定义User-Agent字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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