title: How to set default browser size with Capybara + Selenium + Firefox?
body: <p>Looking at <a href="https://gist.github.com/mars/6957187#gistcomment-3019727" rel="nofollow noreferrer">https://gist.github.com/mars/6957187#gistcomment-3019727</a> and several other places, it appears that the way to change the default options is to overwrite the driver registration with your own code. Here's my driver registration:</p>
  <pre class="lang-rb prettyprint-override"><code>RSpec.configure do |config|
    Capybara.register_driver :selenium_headless do |app|
      version = Capybara::Selenium::Driver.load_selenium
      options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
      browser_options = ::Selenium::WebDriver::Firefox::Options.new.tap do |opts|
        opts.add_argument '-headless'
        opts.add_argument '--window-size=1920,1080'
      end
      Capybara::Selenium::Driver.new(app, **{ :browser =&gt; :firefox, options_key =&gt; browser_options })
    end
  end
  </code></pre>
  <p>I know this is working, as the window size is clearly being passed to Firefox:</p>
  <pre><code>root        1466    1449  0 00:47 pts/5    00:00:00 /root/.webdrivers/geckodriver --port=4444 --binary=/usr/bin/firefox
  root        1469    1466  1 00:47 pts/5    00:00:04 firefox-esr --marionette -headless --window-size=1920,1080 -foreground -no-remote -profile /tmp/rust_mozprofileAPMJOb
  </code></pre>
  <p>But, when I interrupt my tests and then use <code>save_and_open_screenshot</code>, the browser is definitely NOT 1920x1080:</p>
  <pre><code>General
  Complete name                            : capybara-202106300047373170133175.png
  Format                                   : PNG
  Format/Info                              : Portable Network Graphic
  File size                                : 45.1 KiB
  
  Image
  Format                                   : PNG
  Format/Info                              : Portable Network Graphic
  Format_Compression                       : Deflate
  Width                                    : 1 366 pixels
  Height                                   : 694 pixels
  Color space                              : RGBA
  Bit depth                                : 8 bits
  Compression mode                         : Lossless
  Stream size                              : 45.1 KiB (100%)
  </code></pre>
  <p>I'm not sure why the browser appears to be the wrong size. If I manually resize the page, the resulting saved image works fine:</p>
  <pre><code>[9] pry(#&lt;RSpec::ExampleGroups::TheUserLoginAndAvatarArea::ClickingTheDropdownForTheUserProfile&gt;)&gt; page.driver.browser.manage.window.resize_to(1920,1080)
  =&gt; {&quot;x&quot;=&gt;0, &quot;y&quot;=&gt;0, &quot;width&quot;=&gt;1920, &quot;height&quot;=&gt;1080}
  </code></pre>
  <p>But the whole point is to not have to do this.</p>
  <p>I've seen several examples of using <code>resize_to</code> in a <code>before</code>, but that's also not optimal.</p>
  <p>Any thoughts on why this doesn't work as expected?</p>
  <p><strong>Edit:</strong>
  It actually works with Chromium:</p>
  <pre><code>  Capybara.register_driver :selenium_chrome_headless do |app|
      version = Capybara::Selenium::Driver.load_selenium
      options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
      browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
        opts.add_argument('--no-sandbox')
        opts.add_argument('--window-size=1920,1080')
        opts.add_argument('--headless')
        opts.add_argument('--disable-gpu') if Gem.win_platform?
        # Workaround https://bugs.chromium.org/p/chromedriver/issues/detail?id=2650&amp;q=load&amp;sort=-id&amp;colspec=ID%20Status%20Pri%20Owner%20Summary
        opts.add_argument('--disable-site-isolation-trials')
      end
    
      Capybara::Selenium::Driver.new(app, **{ :browser =&gt; :chrome, options_key =&gt; browser_options })
    end
  </code></pre>