问题描述
在使用 Selenium Grid 3 进行自动化测试的过程中,出现“启动停滞”问题(在经过漫长等待后,Selenium Node 才能启动浏览器,开始自动化测试)。
查看 Selenium Hub 日志,发现如下信息:
Sep 28 04:25:41 selenium-hub java[4609]: 04:25:41.020 DEBUG [ProxySet.getNewSession] - Available nodes: [http://172.31.253.104:24663]
Sep 28 04:25:41 selenium-hub java[4609]: 04:25:41.020 DEBUG [BaseRemoteProxy.getNewSession] - Trying to create a new session on node http://172.31.253.104:24663
Sep 28 04:25:41 selenium-hub java[4609]: 04:25:41.021 DEBUG [BaseRemoteProxy.getNewSession] - Node http://172.31.253.104:24663 has no free slots
问题原因
在自动化测试代码中,我们没有正确的退出浏览器(WebDriver),占用 Selenium Node 资源,长此以往导致 Selenium Node 无法分配资源进行新的测试。
应该使用 webDriver.quit() 关闭浏览器,而不是使用 webDriver.close() 关闭浏览器。
webDriver.close()
close() is a webdriver command which closes the browser window which is currently in focus.During the automation process, if there are more than one browser window opened, then the close() command will close only the current browser window which is having focus at that time. The remaining browser windows will not be closed.
webDriver.quit()
quit() is a webdriver command which calls the driver.dispose method, which in turn closes all the browser windows and terminates the WebDriver session. If we do not use quit() at the end of program, the WebDriver session will not be closed properly and the files will not be cleared off memory. This may result in memory leak errors.
解决办法
修改代码,将在自动化测试中的 webDriver.close() 改为 webDriver.quit()