Kevin Bedell
Kevin Bedell

Reputation: 13404

How can I use capybara to test view functionality based on USER_AGENTs

I have some functionality in a rails 3.2 app that needs to be driven by the user's USER_AGENT. Basically, for certain older browsers I need to have the pages render differently.

How can I test that my views respond correctly using using capybara / cucumber?

I saw this post but it doesn't look like it works with the lastest capybara -- it basically hacks the way capybara stubs environmental parameters:

http://blog.sparqcode.com/2011/03/07/custom-user-agent-strings-with-cucumber-and-capybara/

What I'd like is a cucumber step definition that looks something like this:

When /^I visit the page with an unsupported browser$/ do
  user_agent = "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/2008092313 Ubuntu/9.25 (jaunty) Firefox/3.8"
  # do something here that mimics the user arriving on the page with this USER_AGENT
end

Upvotes: 1

Views: 837

Answers (1)

Jon M
Jon M

Reputation: 11705

If you're using Firefox to run your tests, you can create a custom driver with whatever USER_AGENT you like. See this answer.

Once you've created a custom driver, you can use it when running a scenario simply by tagging the scenario with the drivers name, e.g if you do Capybara.register_driver :old_browser, you can tag the scenario as @old_browser, and Capybara will automatically switch drivers. If you don't like that, you can switch drivers at any time you like: Capybara.current_driver = :old_browser.

Upvotes: 1

Related Questions