Chester Tester
Chester Tester

Reputation: 31

The browser window may have been closed. (Selenium::WebDriver::Error::UnknownError)

I have multiple features when run together throw this error. If I run the scenarios by them self there is no issue. I think the issue is that popups are generated during the test run and are not closed properly. I have code in an After hook that closes all open windows except the very first window opened.

Error: Session [2c50a228-3ad7-a544-a6ca-5d173b86bc86] has no driver. The browser window may have been closed. (Selenium::WebDriver::Error::UnknownError)

I have added a bunch of print statements in my code to get the current state:
After:Session -> #<>Capybara::Session:0x00000100f811b8>
Before:Driver -> selenium
Before:Session Object -#<>Capybara::Session:0x00000100f811b8>
Before: (start) Driver Object #<>Capybara::Selenium::Driver:0x000001028ad790>

The scenario before this particular scenario fails, this HAS to be the culprit, but why? Can anyone point me in the right direction?

After hook

#assume ONLY last window opened is to be closed
page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)
page.execute_script "window.close();"

#switch back to first window opened, make it the default window now
page.driver.browser.switch_to.window(page.driver.browser.window_handles.first)  


Env: capybara (2.0.3)
cucumber (1.1.9)
selenium-webdriver (2.29.0)
ruby 1.9.3p0

Upvotes: 0

Views: 3911

Answers (2)

Felipe H.
Felipe H.

Reputation: 21

The solution for this is to maximise the window at the first place. I've not been able to find a way to maximise the window yet. In the middle of the test i manually click the maximise browser button and the test runs perfectly.

https://github.com/fahenao/_bot

Upvotes: 0

Arup Rakshit
Arup Rakshit

Reputation: 118261

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get "https://www.google.co.in/"
address = driver.find_element(:link_text, "Gmail").attribute('href')
driver.execute_script( "window.open()" )
p driver.window_handles.length
p driver.window_handles.first #=> "{f17eac79-daf9-4a6c-a1ff-1b524fef9faf}"
driver.switch_to.window( driver.window_handles.last )
driver.get address
driver.execute_script "window.close()"
driver.execute_script "window.close()"
# => Window not found. The browser window may have been closed. (Selenium::WebDriver::Error::NoSuchWindowError)

In the above code,I just tried to re-generate the error,and it happened.Error is very logical as I tried to close an already closed,non existent window.

Now I want to debug this way:

p driver.browser.window_handles.length # if this is 0,then below line obvious throw error.
page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)
page.execute_script "window.close();"
p driver.browser.window_handles.length # if this is 0,then below line obvious throw error.
page.driver.browser.switch_to.window(page.driver.browser.window_handles.first)

Now debug and let me know your result.

Upvotes: 1

Related Questions