我希望使用可以模拟iphone的用户代理来运行我的一些测试。目前我在FF,Chrome和无头上运行测试,我将驱动程序定义为环境变量,如下所示:
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 =无头,Chrome或无头将启动。我希望使用用户代理做同样的事情,我尝试了以下内容:
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/并试图遵循它但没有运气。
非常感谢任何帮助!!
答案 0 :(得分:2)
要不运行服务器,您应该设置:
Capybara.run_server = false
要将默认驱动程序设置为:iphone,您应该设置:
Capybara.default_driver = :iphone
所以完整的设置将是:
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)
end
Capybara.default_driver = :iphone
Capybara.run_server = false
请注意,最好使用ios-driver或appium代替Firefox并传递用户代理