Capybara does not skip header after submit form

I am creating a Rails 3 application that displays different views for mobile devices (using jQuery Mobile) and regular desktop browsers. I am testing Cucumber / Capybara and have separate test suites for mobile devices and a browser. I can set the User-Agent mobile line in the HTTP request header just fine using the header header found here ...

Using Cucumber / Capybara in Rails 3, how to set a User-Agent user line?

Problem...

Most of my Cucumber mobile steps work fine (for example, I set the title of the mobile device to a step, after which the mobile views are rendered). However, after submitting the form, the test then displays the browser view in the next step (and not the mobile view that I want). I think this could be due to Capybara throwing a header after the form submit action.

I tried to verify this by adding a logger to my controller action so that I could view the user_agent headers as follows:

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

I used the iPhone header (buyer is a 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 next show action (note "Headers: empty) ...

 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 you would expect, "show me the page step, and then display the browser page, not the mobile page I want.

How to save the title so that all my mobile actions are performed in my mobile test suite?

Update

Jnicklas (creator of the truly amazing Capybara stone!) Replies:

"This is more a question than a problem. Tbh. There is currently no way to achieve this disadvantage in Rack-Test, which I know. I was thinking of adding a header option to the rack driver that will allow for the transfer of various header options. If you use a lot of JavaScript, Selenium may be better anyway and allows you to freely install the user agent, IIRC. "

https://github.com/jnicklas

https://github.com/jnicklas/capybara

+45
jquery-mobile header ruby-on-rails-3 cucumber capybara
Apr 20 2018-11-18T00:
source share
2 answers

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

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

+5
Jun 24 '11 at 16:30
source share

Here's how we fixed it to work with javascript (note, use remove_headers in the 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) 
+2
Aug 14 '12 at 23:28
source share



All Articles