【发布时间】:2023-03-31 13:43:01
【问题描述】:
在我新创建的 Django 应用程序中,我将其更改为使用 PostgreSQL,并创建了一个应用程序,我进行了以下测试:
from django.contrib.auth.models import User
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class TestWebBrowser(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.webdriver = webdriver.Chrome()
cls.webdriver.implicitly_wait(10)
@classmethod
def tearDownClass(cls):
cls.webdriver.close()
cls.webdriver.quit()
super().tearDownClass()
def setUp(self):
self.admin = User.objects.create_superuser(username="username", password="password",
email="example@example.com")
def test_log_in(self):
self.webdriver.get(f"{self.live_server_url}/admin")
self.webdriver.find_element_by_id("id_username").send_keys("username")
self.webdriver.find_element_by_id("id_password").send_keys("password")
self.webdriver.find_element_by_id("id_password").send_keys(Keys.RETURN)
self.webdriver.find_element_by_link_text("Users").click()
测试总是运行,Chrome 启动,按照测试说的做,但在最后,有时它会抛出这个错误:
Exception happened during processing of request from ('127.0.0.1', 55283)
Traceback (most recent call last):
File "C:\Users\pupeno\scoop\apps\python\current\lib\socketserver.py", line 647, in process_request_thread
self.finish_request(request, client_address)
File "C:\Users\pupeno\scoop\apps\python\current\lib\socketserver.py", line 357, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Users\pupeno\scoop\apps\python\current\lib\socketserver.py", line 717, in __init__
self.handle()
File "C:\Users\pupeno\Temporary\untitled\venv\lib\site-packages\django\core\servers\basehttp.py", line 139, in handle
self.raw_requestline = self.rfile.readline(65537)
File "C:\Users\pupeno\scoop\apps\python\current\lib\socket.py", line 589, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
所有测试都通过。我只是把它交给STDERR。任何想法为什么?我错过了一些拆解吗?
如果我将tearDownClass 更改为:
@classmethod
def tearDownClass(cls):
cls.webdriver.quit()
我以相同的频率得到相同的错误(据我观察,没有测量它)。
我在跑步:
Django==2.1.2
selenium==3.141.0
和
> chromedriver.exe --version
ChromeDriver 2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a)
和
Google Chrome Version 70.0.3538.102 (Official Build) (64-bit)
运行测试的完整输出如下所示:
Testing started at 15:33 ...
C:\Users\pupeno\Temporary\untitled\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pycharm\django_test_manage.py" test foo.tests.TestImportCRMData C:\Users\pupeno\Temporary\untitled
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 49825)
Traceback (most recent call last):
File "C:\Users\pupeno\scoop\apps\python\current\lib\socketserver.py", line 647, in process_request_thread
self.finish_request(request, client_address)
File "C:\Users\pupeno\scoop\apps\python\current\lib\socketserver.py", line 357, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Users\pupeno\scoop\apps\python\current\lib\socketserver.py", line 717, in __init__
self.handle()
File "C:\Users\pupeno\Temporary\untitled\venv\lib\site-packages\django\core\servers\basehttp.py", line 139, in handle
self.raw_requestline = self.rfile.readline(65537)
File "C:\Users\pupeno\scoop\apps\python\current\lib\socket.py", line 589, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
----------------------------------------
Destroying test database for alias 'default'...
Process finished with exit code 0
【问题讨论】:
-
@DebanjanB:我不认为这是一个重复的问题。另一个是关于无法与 chromedriver 进行通信,而在我的情况下是有效的。错误发生在最后。
-
这就是我得到的所有堆栈跟踪。它必须发生在一个单独的线程上。
-
您能帮我确定您在哪一行看到错误吗?
-
我的代码的任何一行都不会发生这种情况。我的测试都通过了,我只是不时将它发送给
STDERR。 -
您找到解决方案了吗?
标签: python django selenium google-chrome selenium-chromedriver