表单提交后 Capybara 不传递标头

发布于 2024-11-02 10:00:34 字数 1915 浏览 0 评论 0原文

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

在 Rails 3 中使用 Cucumber/Capybara,如何设置自定义User-Agent 字符串?

问题...

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

我尝试通过向我的控制器操作添加一个记录器来测试这一点,以便我可以查看 user_agent 标头,如下所示:

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

我使用了 iPhone 标头(买家是资源)。在我的 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)

正如人们所期望的那样,“向我显示页面”步骤然后呈现浏览器页面,而不是移动页面我想要的页面。

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

更新

Jnicklas(真正令人敬畏的水豚宝石的创造者!)回应:

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

https://github.com/jnicklas

https://github.com/jnicklas/capybara

I am building a Rails 3 app which renders different views for mobile devices (using jQuery Mobile) and regular desktop browsers. I am testing with Cucumber/Capybara, and have separate test suites for mobile and browser. I am able to set a mobile User-Agent string in the request's HTTP header just fine using the ‘headers hack’ found here…

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

The Problem...

Most of my mobile Cucumber steps work fine (e.g., I set the mobile header in a step, thereafter the mobile views render). However after a form submit, the test then renders a browser view of the next step (not the mobile view I want). I think this may be because Capybara is dropping the header after a form submit action.

I tried to test this by adding a logger to my controller action so I could look at the user_agent headers, as follows:

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

I used an iPhone header (Buyer is the resource). In my test.log I see:

For the create action…

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

For the subsequent show action (note ‘Headers:’ is blank)…

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)

As one would expect, the ‘show me the page’ step then renders the browser page, not the mobile page I want.

How can I preserve the header so all my mobile steps run in my mobile test suite?

Update

Jnicklas (creator of the truly awesome Capybara gem!) responds:

“This is more of a question than an issue, tbh. In Rack-Test there is currently there no way of achieving this short of monkey patching that I am aware of. I've been thinking about adding a header option to the rack-test driver, which would allow the passing in of different header options. If you're using a lot of JavaScript, Selenium might be a better fit anyway and allows you to set the user agent freely, IIRC.”

https://github.com/jnicklas

https://github.com/jnicklas/capybara

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风和你 2024-11-09 10:00:34

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

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

This feature was added to Capybara on April 25th, 2011 with this commit - https://github.com/jnicklas/capybara/commit/a00435d63085ac2e74a0f64e7b7dedc0f7252ea9

You can can now specify a custom header when using a custom Capybara driver. See http://automagical.posterous.com/creating-a-custom-capybara-driver for code examples.

往事随风而去 2024-11-09 10:00:34

以下是我们修复此问题的方法,以便也可以使用 javascript(注意,请在 After 块中使用 remove_headers):

module CapybaraHeadersHelper
  def add_headers(headers)
    headers.each do |name, value|
      case page.driver.class.to_s
      when "Capybara::RackTest::Driver"
        page.driver.options[:headers] ||= {}
        page.driver.options[:headers][name.to_s] = value

      when "Capybara::Driver::Webkit"
        page.driver.browser.header name, value

      end
    end
  end

  def remove_headers(headers)
    headers.each do |name|
      case page.driver.class.to_s
      when "Capybara::RackTest::Driver"
        page.driver.options[:headers] ||= {}
        page.driver.options[:headers].delete name.to_s

      when "Capybara::Driver::Webkit"
        page.driver.browser.header name, nil

      end
    end
  end
end

World(CapybaraHeadersHelper)

Here's how we fixed it, to work with javascript as well (note, use remove_headers in your After block):

module CapybaraHeadersHelper
  def add_headers(headers)
    headers.each do |name, value|
      case page.driver.class.to_s
      when "Capybara::RackTest::Driver"
        page.driver.options[:headers] ||= {}
        page.driver.options[:headers][name.to_s] = value

      when "Capybara::Driver::Webkit"
        page.driver.browser.header name, value

      end
    end
  end

  def remove_headers(headers)
    headers.each do |name|
      case page.driver.class.to_s
      when "Capybara::RackTest::Driver"
        page.driver.options[:headers] ||= {}
        page.driver.options[:headers].delete name.to_s

      when "Capybara::Driver::Webkit"
        page.driver.browser.header name, nil

      end
    end
  end
end

World(CapybaraHeadersHelper)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文