Rails 4.2/rspec 3.5/Capybara webkit - 为某些测试设置自定义 http 用户代理时避免 "leak"
Rails 4.2/rspec 3.5/Capybara webkit - Avoid "leak" when setting custom http user agent for some tests
我 90% 的测试都是在 "standard" capybara-webkit 上进行的。
但在一些测试中我需要设置自定义用户代理(因为我们的 javascript 文件为某些浏览器创建自定义 UI,例如 android 三星浏览器或 iphone 火狐...)
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
然后在我的测试中我会写
describe "test to test UI customisation", mobile_device_android_samsung_browser: true
# test things
end
使用这种元数据时,我是否应该确保它不会 "leak" 进入必须继续使用 basic/default webkit 用户代理的其他测试?
在那种情况下,我编写了下面的代码,但我真的不知道 "basic"/默认用户代理应该让测试返回到什么?
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
我应该使用上面的代码吗?是否有自定义方法可以用来描述 "standard"/默认用户代理,例如 default_user_agent?
在 capybara-webkit
中,您只需将用户代理设置为空字符串即可将其重置为默认值
page.driver.header('user-agent', '')
尽管每次重置驱动程序时它都会重置,所以只要您没有在每次测试之间禁用 Capybaras 重置,它就应该恢复为每个新测试的默认值。
另请注意,如果您希望正确设置用户代理,则不应包括前导 'http_'。参见 https://github.com/thoughtbot/capybara-webkit/blob/master/spec/driver_spec.rb#L2018
我 90% 的测试都是在 "standard" capybara-webkit 上进行的。
但在一些测试中我需要设置自定义用户代理(因为我们的 javascript 文件为某些浏览器创建自定义 UI,例如 android 三星浏览器或 iphone 火狐...)
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
然后在我的测试中我会写
describe "test to test UI customisation", mobile_device_android_samsung_browser: true
# test things
end
使用这种元数据时,我是否应该确保它不会 "leak" 进入必须继续使用 basic/default webkit 用户代理的其他测试?
在那种情况下,我编写了下面的代码,但我真的不知道 "basic"/默认用户代理应该让测试返回到什么?
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
我应该使用上面的代码吗?是否有自定义方法可以用来描述 "standard"/默认用户代理,例如 default_user_agent?
在 capybara-webkit
中,您只需将用户代理设置为空字符串即可将其重置为默认值
page.driver.header('user-agent', '')
尽管每次重置驱动程序时它都会重置,所以只要您没有在每次测试之间禁用 Capybaras 重置,它就应该恢复为每个新测试的默认值。
另请注意,如果您希望正确设置用户代理,则不应包括前导 'http_'。参见 https://github.com/thoughtbot/capybara-webkit/blob/master/spec/driver_spec.rb#L2018