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 ?
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