Updated: How to allow testing beforeunload confirmation dialogs with modern ChromeDrivers

Posted . Visible to the public. Auto-destruct in 35 days

Updated for selenium-webdriver 4.27 which requires options.add_option(:page_load_strategy, 'none').

Changes

    ...
  • if unload_confirmation
  • options.add_option(:web_socket_url, true)
  • + options.add_option(:page_load_strategy, 'none') # required for selenium-webdriver 4.27+
  • end
  • Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
  • ...
  • In combination with `unhandled_prompt_behavior: 'ignore'` ([which we recommend setting](https://makandracards.com/makandra/617366-configure-selenium-webdriver-automatically-close-alerts)) any unload alerts will now stay open until handled.
  • +Note that you also need to set the `:page_load_strategy` to `"none"` for modern selenium-webdriver ≥ 4.27 since those follow the spec more closely than previous versions and respect the document's [readiness state](https://www.w3.org/TR/webdriver-bidi/#type-browsingContext-ReadinessState) for navigation events.
  • +If you do not specify a `:page_load_strategy`, selenium-webdriver defaults to `"complete"` which means it will wait for any `beforeunload` to be handled before e.g. a `page.refresh` statement returns. That that this also applies to wrapped calls like `accept_confirm { page.refresh }`.
  • +
  • ## Recommendation
  • We **do not recommend BiDi mode as a default** because it can have a negative impact on other tests.
  • ...
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2024-12-16 11:33)