Rails 4.2/rspec 3.5/Capybara webkit - 为某些测试设置自定义 http 用户代理时避免"leak"



90%的测试都是使用"标准"水豚网络套件进行的。

但是在一些测试中,我需要设置自定义用户代理(因为我们的 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

使用这种元数据时,我是否应该确保它不会"泄漏"到必须继续使用基本/默认 webkit 用户代理的其他测试中?

在这种情况下,我

编写了下面的代码,但我真的不知道我应该让测试回到什么"基本"/默认用户代理?

  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

我应该使用上面的代码吗,是否有一种自定义方法来描述"标准"/默认用户代理,例如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

最新更新