使用headless_chrome模拟设备

时间:2018-07-19 08:04:52

标签: ruby rspec capybara selenium-chromedriver headless-browser

我正在运行规格时尝试仿真不同的设备,但目前没有结果。

#spec_helper

require 'rspec'
require 'capybara'
require 'capybara/rspec'
require 'selenium/webdriver'

Capybara.register_driver :headless_chrome do |app|
 Capybara::Selenium::Driver.load_selenium
 browser_options = ::Selenium::WebDriver::Chrome::Options.new
 browser_options.args << '--headless'

 mobile_emulation = { "deviceName" => "iPhone 8" }
 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
   "chromeOptions" => { "mobileEmulation" => mobile_emulation }
 )

 Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options, desired_capabilities: capabilities)
end

我应该在选项中包括mobile_emulation部分而不是功能吗?

1 个答案:

答案 0 :(得分:1)

chromeOptions不再是有效的密钥(已替换为goog:chromeOptions),但是由于您已经在使用::Selenium::WebDriver::Chrome::Options类,因此应该只使用add_emulation方法-https://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/Chrome/Options#add_emulation-instance_method-

browser_options = ::Selenium::WebDriver::Chrome::Options.new
browser_options.args << '--headless'
browser_options.add_emulation(device_name: 'iPhone 8')

Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)