title: Ensure that a popup has been closed with Capybara/Selenium
body: <p>I am attempting to write a capybara spec which opens a popup, and then asserts that the popup has closed itself.  </p>
  
  <pre><code>visit "/my_page"
  
  click_button "Open My Popup"
  
  within_window("MyPopup") do
    # test things on MyPopup
  end
  
  sleep 1 # long enough that window.close() has been called in the popup
  
  # ensure that MyPopup is closed
  </code></pre>
  
  <p>Things I've tried so far...</p>
  
  <p>Counting the window handles:<br>
  <code>page.driver.browser.window_handles.length</code><br>
  This remains at 2 even after the popup is closed</p>
  
  <p>As <a href="https://stackoverflow.com/questions/15428831/detecting-the-closing-of-a-popup-in-chrome-using-selenium">this post</a> suggests, check for the visibility of an element on the popup.  This doesn't seem to work.  Even though the popup has been closed, I can run assertions against the window as if it's still there, just as before:  </p>
  
  <pre><code>within_window("MyPopup") do
    # test things on MyPopup
  end
  </code></pre>
  
  <p>I'm ensured that window.close is firing from the popup (by adding some JS that modifies the popup's DOM after the window.close() and asserting on those changes).</p>
  
  <p>I'm fairly stumped here; anyone out there know the secret sauce for asserting that the window actually closed?</p>