「Selenium Grid 3」- Node xxxxx has no free slots(close() vs quit())

  CREATED BY JENKINSBOT

问题描述

在使用 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()

附加说明

如果使用 webDriver.close() 将会发现:虽然没有正在运行的自动化测试任务,Selenium Node 依旧存在正在运行的 chromedriver 进程。这就是因为没有正确关闭(webDriver.quit()) WebDriver 而出现的问题。

参考文献

python – How do I close the browser window at the end of a Selenium test? – Software Quality Assurance & Testing Stack Exchange
What is close() and quit() commands in Selenium Webdriver? | Zyxware Technologies
selenium close browser- How to close the whole browser window by keeping the WebDriver active? – Intellipaat Community