Rails 用水豚模拟移动设备

Rails simulate mobile device with capybara

如何使用水豚模拟移动设备视图?

我当前的密码是

require 'spec_helper'

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

feature 'User', integration: true, :js => true do
  scenario 'favorite products' do
    4.times { create(:advices) }
    category = create(:category)
    sub_category = create(:subcategory, category_id: category.id)
    brand = create(:brand, title: 'Fender')
    create(:banner, position: 1)

    visit '/'
    binding.pry
  end
end

我应该怎么做才能模拟移动视图?

您是否想看看它在移动设备上的显示效果?

您可以使用 #resize_to 方法将其调整为移动设备,如 Capybara.current_se‌​ssion.current_window.‌​resize_to(768, 480)

在此处找到 http://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FWindow%3Aresize_to

Here 您可以找到针对不同分辨率使用不同驱动程序名称的示例。此外,它还使用 args 更改分辨率和用户代理。例如:

args = []
args << "–window-size=320,480"
args << "–user-agent='Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3'"
Capybara::Selenium::Driver.new(app, :browser => :chrome, :args => args)

Chrome 驱动程序通过 mobileEmulation 功能支持移动仿真:

require 'capybara'
require 'selenium-webdriver'

Capybara.register_driver :iphone do |app|
  Capybara::Selenium::Driver.new(app,
    :browser => :chrome,
    :desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome(
      'chromeOptions' => {
        'mobileEmulation' => {
          'deviceMetrics' => { 'width' => 360, 'height' => 640, 'pixelRatio' => 3.0 },
          'userAgent' => "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"
        }
      }
    )
  )
end

session = Capybara::Session.new(:iphone)
session.visit "http://whosebug.com/"

在水豚测试期间,我在移动仿真模式下将 chrome 设置为 运行 时遇到问题。如果有人遇到类似问题,您必须在 Capybara 驱动程序选项中将 'chromeOptions' 键更改为 'goog:chromeOptions',因为它在 Selenium 中以这种方式更改。

尝试https://github.com/renuo/so_many_devices,已更新:

This gem provides a list of Capybara Selenium configurations that you can use. Probably useful to run your system tests on different devices.