单击后 Selenium 给出服务器错误 500(Django,pytest)
Selenium gives Server Error 500 after click (Django, pytest)
我有一个 Django 应用程序,我想通过 pytest 和 Selenium 进行测试。
我尝试执行的例程是让 Selenium 登录,然后转到菜单,选择其中一个选项。这将重定向到一个新页面,发现它很好。在那里,我让 selenium 输入数据并单击一个按钮开始提交。这也有效,并重定向到第三页。
在第三个“您的提交已成功创建”页面上,显示了一个 link 用户可以单击以收集他们提交的结果(此页面。显示此 link正确地,href URL 没问题。但是当我让 Selenium 单击它时,我突然得到一个 Server Error 500
:
<html lang="en"><head>
<title>Server Error (500)</title>
</head>
<body>
<h1>Server Error (500)</h1><p></p>
</body></html>
当我手动执行完全相同的操作时,效果很好。
这是我的测试代码(稍微简化):
@pytest.fixture(scope="class")
def chrome_driver_init(request):
options = webdriver.ChromeOptions()
options.headless = True
options.binary_location = CHROME_BIN_PATH
driver = webdriver.Chrome(service=CHROME_SERVICE, options=options)
request.cls.driver = driver
yield
driver.quit()
@pytest.mark.django_db
@pytest.mark.usefixtures("chrome_driver_init")
class SubmissionTest(LiveServerTestCase):
def test_adding_submission(self):
self.driver.get(self.live_server_url)
username = TEST_USER
pwd = TEST_USER_PWD
User = get_user_model()
user = User.objects.create_user(username=username, password=pwd)
user.save()
# click 'Login':
self.driver.find_element(By.LINK_TEXT, "Login").click()
# now on Login page, log in via Selenium:
username_field = self.driver.find_element(By.NAME, "username")
pwd_field = self.driver.find_element(By.NAME, "password")
submit_btn = self.driver.find_element(By.ID, "form_field")
username_field.send_keys(TEST_USER)
pwd_field.send_keys(TEST_USER_PWD)
submit_btn.click()
# now logged in, go to desired menu point:
self.driver.find_element(By.LINK_TEXT, "MenuPoint 1").click()
assert self.driver.title == "MenuPoint 1" # redirection works fine
## find various fields, enter stuff, click "submit"
# check successfull:
assert self.driver.title == "Added Submission" # redirection works, as expected
## more checks => yes, this is the desired page
# find link for result page:
result_link = self.driver.find_element(By.LINK_TEXT, "click here for your results")
result_link_url = result_link.get_attribute("href")
print(result_link_url) # this is indeed the correct URL
# click link:
time.sleep(10) # wait 10 seconds => more did not help, either
result_link.click()
## now we get the Server Error :-(
print(self.driver.page_source)
我已经尝试使用 WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "click here for your results"))).click()
,但似乎连点击都没有。
我知道错误 500 表示错误在服务器端。但是在点击之前,服务器是好的。此步骤之前的所有重定向都可以正常工作。 URL 也签出。手动,一切都按预期工作。我不知道如何进一步缩小问题范围。
我如何调查此错误的原因并希望修复它?
测试仅创建一个用户实例。对于失败的视图,这是否足够?
您需要访问服务器回溯才能确定出了什么问题。
from django.test import override_settings
@pytest.mark.django_db
@pytest.mark.usefixtures("chrome_driver_init")
class SubmissionTest(LiveServerTestCase):
@override_settings(DEBUG=True)
def test_adding_submission(self):
...
查看 Django: why i can't get the tracebacks (in case of error) when i run LiveServerTestCase tests? 了解更多详情。
我有一个 Django 应用程序,我想通过 pytest 和 Selenium 进行测试。
我尝试执行的例程是让 Selenium 登录,然后转到菜单,选择其中一个选项。这将重定向到一个新页面,发现它很好。在那里,我让 selenium 输入数据并单击一个按钮开始提交。这也有效,并重定向到第三页。
在第三个“您的提交已成功创建”页面上,显示了一个 link 用户可以单击以收集他们提交的结果(此页面。显示此 link正确地,href URL 没问题。但是当我让 Selenium 单击它时,我突然得到一个 Server Error 500
:
<html lang="en"><head>
<title>Server Error (500)</title>
</head>
<body>
<h1>Server Error (500)</h1><p></p>
</body></html>
当我手动执行完全相同的操作时,效果很好。
这是我的测试代码(稍微简化):
@pytest.fixture(scope="class")
def chrome_driver_init(request):
options = webdriver.ChromeOptions()
options.headless = True
options.binary_location = CHROME_BIN_PATH
driver = webdriver.Chrome(service=CHROME_SERVICE, options=options)
request.cls.driver = driver
yield
driver.quit()
@pytest.mark.django_db
@pytest.mark.usefixtures("chrome_driver_init")
class SubmissionTest(LiveServerTestCase):
def test_adding_submission(self):
self.driver.get(self.live_server_url)
username = TEST_USER
pwd = TEST_USER_PWD
User = get_user_model()
user = User.objects.create_user(username=username, password=pwd)
user.save()
# click 'Login':
self.driver.find_element(By.LINK_TEXT, "Login").click()
# now on Login page, log in via Selenium:
username_field = self.driver.find_element(By.NAME, "username")
pwd_field = self.driver.find_element(By.NAME, "password")
submit_btn = self.driver.find_element(By.ID, "form_field")
username_field.send_keys(TEST_USER)
pwd_field.send_keys(TEST_USER_PWD)
submit_btn.click()
# now logged in, go to desired menu point:
self.driver.find_element(By.LINK_TEXT, "MenuPoint 1").click()
assert self.driver.title == "MenuPoint 1" # redirection works fine
## find various fields, enter stuff, click "submit"
# check successfull:
assert self.driver.title == "Added Submission" # redirection works, as expected
## more checks => yes, this is the desired page
# find link for result page:
result_link = self.driver.find_element(By.LINK_TEXT, "click here for your results")
result_link_url = result_link.get_attribute("href")
print(result_link_url) # this is indeed the correct URL
# click link:
time.sleep(10) # wait 10 seconds => more did not help, either
result_link.click()
## now we get the Server Error :-(
print(self.driver.page_source)
我已经尝试使用 WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "click here for your results"))).click()
,但似乎连点击都没有。
我知道错误 500 表示错误在服务器端。但是在点击之前,服务器是好的。此步骤之前的所有重定向都可以正常工作。 URL 也签出。手动,一切都按预期工作。我不知道如何进一步缩小问题范围。
我如何调查此错误的原因并希望修复它?
测试仅创建一个用户实例。对于失败的视图,这是否足够?
您需要访问服务器回溯才能确定出了什么问题。
from django.test import override_settings
@pytest.mark.django_db
@pytest.mark.usefixtures("chrome_driver_init")
class SubmissionTest(LiveServerTestCase):
@override_settings(DEBUG=True)
def test_adding_submission(self):
...
查看 Django: why i can't get the tracebacks (in case of error) when i run LiveServerTestCase tests? 了解更多详情。