June 5, 2016 #rspec #poltergeist

The default window size for poltergeist is 1024 x 768, in some cases you may want to change it to fit your specs.

Change Window Size Per Spec

page.driver.resize_window 1200, 768

Then you can check it this way.

page.driver.browser.client.window_size # => [1200, 768]

Note that this will not change the default window size for other specs, so no need to resize it back.

Change Window Size Globally

If you're using RSpec, you may want to create a metadata tag for it.

RSpec.configure do |config|
  config.before(:example, :mobile, type: :feature) do
    page.driver.add_headers("User-Agent" => "iPhone")
    page.driver.resize_window 320, 568
  end
end

Here we registered a :mobile metadata tag to change the window size to 320, 568, also set the user agent to further simulate the request from a mobile.

scenario "Do something in mobile", :js, :mobile do
  page.driver.browser.client.window_size # => [320, 568]
end

Change Default Window Size

You could also customize the window size when register the poltergeist drive.

# spec/support/capybara.rb
require 'capybara/poltergeist'

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, window_size: [1600, 768])
end
Capybara.javascript_driver = :poltergeist