我希望使用模拟 iphone 的用户代理运行我的一些测试。目前我在 FF、Chrome 和 headless 上运行我的测试,我将驱动程序定义为环境变量,如下所示:
when 'chrome'
Capybara.default_selector = :css
Capybara.default_driver = :selenium
Capybara.run_server = false
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
when 'headless'
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.run_server = false
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {:inspector => true})
end
因此,当我运行测试时,我只需输入 BROWSER=chrome 或 BROWSER=headless 并且 chrome 或 headless 将启动。我希望使用用户代理做同样的事情,我尝试了以下方法:
when 'iphone'
Capybara.register_driver :iphone do |app|
require 'selenium/webdriver'
profile = Selenium::WebDriver::Firefox::Profile.new
profile['general.useragent.override'] = "iphone"
Capybara::Driver::Selenium.new(app, :profile => profile)
但是,当我尝试像这样运行测试时:BROWSER=iphone,我收到一条消息“机架测试需要机架应用程序,但没有给出 (ArgumentError)”。因此,我尝试通过在开始时添加默认驱动程序和 javascript 驱动程序来解决此问题,如下所示:
when 'iphone'
Capybara.default_selector = :css
Capybara.default_driver = :selenium
Capybara.run_server = false
Capybara.register_driver :iphone do |app|
require 'selenium/webdriver'
profile = Selenium::WebDriver::Firefox::Profile.new
profile['general.useragent.override'] = "iphone"
Capybara::Driver::Selenium.new(app, :profile => profile)
但这只会导致默认的 FF 浏览器被启动。有谁知道我将如何让它工作或我做错了什么?我找到了这篇文章:http ://blog.plataformatec.com.br/2011/03/configuring-user-agents-with-capybara-selenium-webdriver/并试图遵循它但没有运气。
任何帮助将不胜感激!!