0

一起运行时我有多个功能会抛出此错误。如果我自己运行场景,则没有问题。我认为问题是在测试运行期间会生成弹出窗口并且没有正确关闭。我在 After 钩子中有代码,它会关闭所有打开的窗口,除了打开的第一个窗口。

错误:会话 [2c50a228-3ad7-a544-a6ca-5d173b86bc86] 没有驱动程序。浏览器窗口可能已关闭。(Selenium::WebDriver::Error::UnknownError)

我在代码中添加了一堆打印语句来获取当前状态:
After:Session -> #<>Capybara::Session:0x00000100f811b8>
Before:Driver -> selenium
Before:Session Object -#<>Capybara::Session :0x00000100f811b8>
之前:(开始)驱动程序对象#<>Capybara::Selenium::Driver:0x000001028ad790>

在这个特定场景失败之前的场景,这一定是罪魁祸首,但为什么呢?谁能指出我正确的方向?

钩后

#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)  


环境:capybara (2.0.3)
cucumber (1.1.9)
selenium-webdriver (2.29.0)
ruby​​ 1.9.3p0

4

2 回答 2

1
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)

在上面的代码中,我只是尝试重新生成错误,它发生了。Error当我试图关闭一个已经关闭、不存在的窗口时,这是非常合乎逻辑的。

现在我想这样调试:

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)

现在调试并让我知道你的结果。

于 2013-04-22T17:16:02.873 回答
0

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

于 2016-02-11T22:43:39.063 回答