1

90% of my tests happen with the "standard" capybara-webkit.

But on a few tests I need to set custom user agents(as our javascript file create custom UI for some browsers such as android samsung browser or iphone firefox...)

rspec_helper.rb

RSpec.configure do |config|
  config.before(:each, mobile_device_android_samsung_browser: true) do
    page.driver.header 'HTTP_USER_AGENT', 'User agent string for samsung browser'
  end
  config.before(:each, mobile_device_ios_firefox: true) do
    page.driver.header 'HTTP_USER_AGENT', 'User agent string for ios firefox'
  end

  # and so on... for about 20 cusotm device/user-agents

end

then on my tests i will write

describe "test to test UI customisation", mobile_device_android_samsung_browser: true
  # test things
end

When using this kind of metadata, should I we make sure it does not "leak" into other tests that must continue to use the basic/default webkit user-agent ?

In that case I wrote the code below but I don't really know what the "basic"/default user-agent i should make the tests go back to ?

  config.after(:each, mobile_device_android_samsung_browser: true) do
     page.driver.header 'HTTP_USER_AGENT', 'DEFAULT USER AGENT TO GO BACK TO'
  end
  config.after(:each, mobile_device_ios_firefox: true) do
     page.driver.header 'HTTP_USER_AGENT', 'DEFAULT USER AGENT TO GO BACK TO'
  end
   # do this for the otehr 20 custom user-agents

Should I use the code above and is there a custom method I could use to describe the "standard"/default user-agent such as default_user_agent ?

1 Answer 1

1

In capybara-webkit you can reset the the user agent to default by just setting it to an empty string

page.driver.header('user-agent', '')

although it gets reset every time the driver is reset, so as long as you haven't disabled Capybaras reset between each test it should be back to default for each new test.

Also note you should not be including the leading 'http_' if you want the user agent set correctly. See https://github.com/thoughtbot/capybara-webkit/blob/master/spec/driver_spec.rb#L2018

5
  • When you say "disable capybara reset" you mean using sth like .reset_sessions! (rubydoc.info/github/jnicklas/capybara/Capybara.reset_sessions!) ? I don't . So I could do without even setting the user-agent back to the default one. Cool
    – Mathieu
    Apr 4, 2018 at 16:53
  • @Mathieu Correct, a normal Capybara configuration automatically calls reset_session! after each test. So if you are using a normal Capybara configuration then you shouldn't need to manually reset the user agent yourself. Apr 4, 2018 at 16:56
  • @Mathieu If this solved your issue, don't forget to accept the answer (the checkbmark) so the question gets marked as answered Apr 4, 2018 at 18:57
  • Sure, out of home but I always try the solution then upvote if it's ok. You never know if the specifics can make a solution less relevant or maybe help to get to a more precise answer. Will update tomorrow. Thanks again
    – Mathieu
    Apr 4, 2018 at 19:27
  • Worked perfectly.
    – Mathieu
    Apr 5, 2018 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.