简体   繁体   中英

Resize Chrome's window while using Capybara

I use Capybara (with selenium) and Chrome, and RSpec. But i would like to change width of browser in some tests. What is the solution in this case?

spec/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

There is resize_to function in Selenium Webdriver that you can use:

page.driver.browser.manage.window.resize_to(1024, 768)

Window has some other methods that may be useful.

I've found that the Driver#browser call is now deprecated (Capybara 2.5.0).

You can use the Driver#resize_window_to call to resize the window:

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

You can also use Session#current_window then Window#resize_to :

page.current_window.resize_to(1_200, 800)

Look at the docs for more info:

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

Late answer but someone may need this. You can initiate chromedriver by giving windows size so that you don't need to size it after loading the page:

Capybara.register_driver :chrome_sizes do |app|
  profile = Selenium::WebDriver::Chrome::Profile.new 

  Capybara::Selenium::Driver.new(app,
    :browser => :chrome,
    :profile => profile,
    :args => ["--window-size=1240,1400"]
  )
end

Try this:

it "testname", :js => true do
  set_selenium_window_size(1280, 800) if Capybara.current_driver == :selenium 
  # Resize window. NB: cannot be moved to before :all block, because there Capybara.current_driver will be :webkit. It changes to :selenium inside :js => true blocks.
  # ... the rest of your test ...
end

def set_selenium_window_size(width, height)
  window = Capybara.current_session.driver.browser.manage.window
  window.resize_to(width, height)
end

Thanks to Sidane for his gist https://gist.github.com/Sidane/2204218

Just in case anyone still needs help with this issue:

Capybara.register_driver :selenium_chrome do |app|
    Capybara::Selenium::Driver.new(app, browser: :chrome, options: Selenium::WebDriver::Chrome::Options.new(args: ['--window-size=1920,1080'])) end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM