I recently added a beforeunload event handler to a site I’m working on, which instantly caused a regression of the entire Lettuce test suite before they got stuck on a “Leave this page?” dialog. We reuse the same Selenium browser session between tests in order to make our tests run in something approximating less than a decade.
Unfortunately Ghostdriver can’t see alerts and dialogs, which makes simply dismissing the dialog in Selenium kind of hard, but an easier way is at the end of the scenario to simply disable the event, and let it be reinstated with the next page load.
Add this hook to your Lettuce steps:
@after.each_scenario def disable_beforeunload(scenario): """ Disable before unload after a scenario so that the next scenario can reload the site. """ world.browser.execute_script(""" try { $(window).off('beforeunload'); } catch (e) { } """)