5

I'm trying to emulate different devices when running my specs, with no results for now.

#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

Should I include the mobile_emulation part in the Options instead of Capabilities?

fabdurso
  • 2,366
  • 5
  • 29
  • 55

1 Answers1

2

chromeOptions is no longer a valid key (it was replaced with goog:chromeOptions) but since you're already using the ::Selenium::WebDriver::Chrome::Options class you should just use the add_emulation method - https://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/Chrome/Options#add_emulation-instance_method -on that.

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)
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78