使用Capybara时调整Chrome窗口的大小

Exi*_*iRe 7 selenium rspec google-chrome ruby-on-rails capybara

我使用Capybara(含硒)和Chrome,以及RSpec.但我想在一些测试中改变浏览器的宽度.这种情况下的解决方案是什么?

投机/ spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

# Using chrome as browser to test in capybara.
Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

# RSpec config.
RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller    
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = false

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Clean test database.
  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end
Run Code Online (Sandbox Code Playgroud)

And*_*lov 11

resize_to硒webdriver的功能,您可以使用:

page.driver.browser.manage.window.resize_to(1024, 768)
Run Code Online (Sandbox Code Playgroud)

Window有一些其他可能有用的方法.


bre*_*dan 5

我发现该Driver#browser调用现已弃用(Capybara 2.5.0)。

您可以使用Driver#resize_window_to调用来调整窗口大小:

page.driver.resize_window_to(page.driver.current_window_handle, 1_200, 800)

您还可以使用Session#current_windowthen Window#resize_to

page.current_window.resize_to(1_200, 800)

查看文档以获取更多信息:

http://www.rubydoc.info/github/jnicklas/capybara/Capybara