CDub
CDub

Reputation: 13354

Change default Capybara browser window size

So, with respect to integration testing using Capybara and RSpec, I know I can do this:

page.driver.browser.manage.window.resize_to(x,y)

per How to set Browser Window size in Rspec (Selenium) for specific RSpec tests, but is there a way to do this globally so that every test that is affected by media queries doesn't have to define this?

Upvotes: 36

Views: 27573

Answers (5)

ilgam
ilgam

Reputation: 4430

For test runtime in Capybara 2.2.4 version you can achieve this by doing

before do
  handle = Capybara.page.driver.current_window_handle
  Capybara.page.driver.resize_window_to(handle, width, height)
end

Or

before do   
  Capybara.page.current_window.resize_to(width, height)
end

If you get Capybara::NotSupportedByDriverError: Capybara::Driver::Base#current_window_handle YOU MUST CHANGE YOUR DRIVER FOR EXAMPLE USE JAVASCRIPT DRIVER!

before do   
  Capybara.page.current_window.resize_to(width, height)
end

scenario js: true do
  # your test here
end

Upvotes: 11

Nico Brenner
Nico Brenner

Reputation: 737

@tirdadc if you're using Poltergeist, you can add something like this to your rails_helper.rb file:

Capybara.register_driver :poltergeist do |app|
  options = {
    # js_errors: true,
    # cookies: true,
    window_size: [320, 568] # iphone 5
  }
  Capybara::Poltergeist::Driver.new(app, options)
end

Upvotes: 2

rattray
rattray

Reputation: 6039

Perhaps due to a recent change in Capybara, what worked for me was:

before do
  Capybara.page.current_window.resize_to(x, y)
end

Upvotes: 16

Mike Szyndel
Mike Szyndel

Reputation: 10592

A proper way to do it for all js tests is to add following inside spec_helper.rb RSpec.configure block

config.before(:each, js: true) do
  Capybara.page.driver.browser.manage.window.maximize
end

to maximize the window. Change to resize_to(x,y) to set any window size.

EDIT: If you happen to be using Poltergeist the correct way to do it is

config.before(:each, js: true) do
  Capybara.page.driver.browser.resize(x,y)
end

Upvotes: 39

Amey
Amey

Reputation: 8548

You could define that under before(:all)

describe "Test" do
  before(:all) do
    ...
    ...
    page.driver.browser.manage.window.resize_to(x,y) #Mention it here
  end

  it "should find everything" do
    ...
  end

  after(:all) do
   ...
  end
end

Upvotes: 24

Related Questions