python - 使用 python/django 关闭套接字并避免 Errno 10054 的正确方法是什么

标签 python django unit-testing selenium

我正在与 django-selenium 合作在我参与的 django 应用程序上运行 Selenium 测试。此 django 应用程序使用 Qt 和本地网络服务器在本地运行。

因此,要运行测试,我需要启动应用程序服务器、selenium 服务器,然后启动 webdriver 实例来执行测试。

django-selenium 使用 subprocess.Popen('java -jar <path_to_server.jar>') 设置它的服务器然后,如果服务器没有运行,我会以类似的方式为应用程序运行我们的网络服务器;

def run():
    path = os.path.join(os.getcwd(), 'main.py')
    server_running = is_server_running()

    if server_running is False:
        subprocess.Popen(['python', path, '-a'])

现在在测试设置中看起来像这样;

def setUp(self):
    self.server = Process(target= startServer.run)
    self.server.start()

并拆解;

def tearDown(self):
    # stop our server
    self.ff.get('http://localhost:{0}/QUIT'.format(settings.LISTEN_PORT))
    # stop the selenium server
    self.ff.get('http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer')
    # close the browser
    self.ff.quit()
    self.server.terminate()

现在这样做,我得到一个 error: [Errno 10054] An existing connection was forcibly closed by the remote host 。我尝试添加 sleep在关闭连接的调用之间,但这没有帮助。

你能看出我可能在哪里犯了错误吗?我想如果关闭来自远程主机,那么如果我先关闭我们的服务器,然后关闭 selenium 服务器,然后在服务器关闭后终止进程,那么应该不会有问题。

最佳答案

我也遇到了这个问题,我通过在退出之前刷新浏览器来修复它。 (是的,很奇怪,我知道)。尝试在 self.ff.quit() 之前添加此行:

self.ff.refresh()
self.ff.quit()

这为我解决了这个问题,尽管我不知道为什么。

关于python - 使用 python/django 关闭套接字并避免 Errno 10054 的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15244153/

相关文章:

python - 循环遍历表单 POST 将数据存储在 python 变量中

java - 单元测试一个整体的主要方法

python - 如何一次快速设置 Django 模型的所有实例的字段?

python - Pathos 无法腌制由 GDAL 模块创建的 SwigPyObject

python - 我可以使用变量的值作为函数的参数名称吗?

python - 使用 Python 请求通过 AJAX 表单

python - Django 投票应用程序 : AttributeError: 'Choice' object has no attribute 'question_text'

ios - 如何对以下方法进行单元测试?

java - 单元测试应该默认使用 'throws Exception' 吗?

python - 在不移除拆分器的情况下拆分字符串