diff --git a/.rubocop.yml b/.rubocop.yml index 8a0dde7f..7711a626 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -55,7 +55,7 @@ Metrics/ModuleLength: Max: 602 Metrics/PerceivedComplexity: - Max: 14 + Max: 15 plugins: - rubocop-rake diff --git a/CHANGELOG.md b/CHANGELOG.md index ca9cc072..d8c180bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ - Ferrum is bumped to `~> 0.19.0` ### Fixed +- `window_size: nil` raised `Ferrum::BrowserError: Invalid parameters` on every page instead of leaving the window + size to Chrome, so `start-maximized` couldn't be used [#278] - `Driver#reset!` kept the page it had cached when the browser failed to reset, so every later example raised `Session with given id not found` or `Browser is dead or given window is closed` instead of starting on a fresh page [#274], [rubycdp/ferrum#641] diff --git a/docs/2-customization.md b/docs/2-customization.md index da8de800..64d444d5 100644 --- a/docs/2-customization.md +++ b/docs/2-customization.md @@ -58,6 +58,9 @@ Capybara::Cuprite::Driver.new(app, timeout: 15, process_timeout: 30) # Connect to a remote browser Capybara::Cuprite::Driver.new(app, url: "http://chrome:3333") +# Leave the window size to Chrome, e.g. to start maximized in headful mode +Capybara::Cuprite::Driver.new(app, headless: false, window_size: nil, browser_options: { "start-maximized" => nil }) + # Emulate an iPhone 14 Capybara::Cuprite::Driver.new(app, emulate: :iphone_14) diff --git a/docs/9-windows-frames.md b/docs/9-windows-frames.md index dd162ff7..10579bb3 100644 --- a/docs/9-windows-frames.md +++ b/docs/9-windows-frames.md @@ -54,7 +54,8 @@ Returns inner width and height of the window. page.current_window.size # => [1024, 768] ``` -The window size is reset to `:window_size` at the end of every test. +The window size is reset to `:window_size` at the end of every test. With `window_size: nil` Cuprite doesn't +set the size, so Chrome's own one, or `start-maximized`, applies. ## Frames diff --git a/lib/capybara/cuprite/page.rb b/lib/capybara/cuprite/page.rb index ce7d3e43..6b4fc002 100644 --- a/lib/capybara/cuprite/page.rb +++ b/lib/capybara/cuprite/page.rb @@ -178,7 +178,7 @@ def prepare_page super width, height = @options.window_size - resize(width: width, height: height) + width && height ? resize(width: width, height: height) : set_viewport command("Network.setUserAgentOverride", userAgent: default_user_agent) if @options.device_user_agent if @options.url_blacklist.any? diff --git a/spec/features/driver_spec.rb b/spec/features/driver_spec.rb index d0120972..aa3bd484 100644 --- a/spec/features/driver_spec.rb +++ b/spec/features/driver_spec.rb @@ -230,6 +230,25 @@ def session_url(path) driver&.quit end + it "leaves the window size to the browser when the option is nil" do + Capybara.register_driver :cuprite_without_window_size do |app| + Capybara::Cuprite::Driver.new(app, window_size: nil) + end + driver = Capybara::Session.new(:cuprite_without_window_size, TestApp).driver + driver.visit(session_url("/")) + default_size = driver.evaluate_script("[window.innerWidth, window.innerHeight]") + expect(default_size).not_to eq([1024, 768]) + + driver.resize(800, 600) + expect(driver.evaluate_script("[window.innerWidth, window.innerHeight]")).to eq([800, 600]) + + driver.reset! + driver.visit(session_url("/")) + expect(driver.evaluate_script("[window.innerWidth, window.innerHeight]")).to eq(default_size) + ensure + driver&.quit + end + shared_examples "render screen" do it "supports rendering the whole of a page that goes outside the viewport" do @session.visit("/cuprite/long_page")