我正在尝试在运行我的规范时模拟不同的设备,现在没有结果.
#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部分吗?
最佳答案 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)