我有一個 Django 應用程式,我想通過 pytest 和 Selenium 進行測驗。
我試圖通過的例程是讓 Selenium 登錄,然后轉到選單,選擇其中一個選項。這將重定向到一個新頁面,該頁面發現正常。在那里,我讓 selenium 輸入資料并單擊按鈕開始提交。這也有效,并重定向到第三頁。
在第三個“您的提交已成功創建”頁面上,顯示了一個鏈接,用戶可以單擊該鏈接來收集他們提交的結果(此頁面。此鏈接顯示正確,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 意味著錯誤在服務器端。但是在點擊之前,服務器是好的。此步驟之前的所有重定向都可以正常作業。網址也簽出。手動,一切都按預期作業。我不知道如何進一步縮小問題的范圍。
我怎樣才能調查這個錯誤的原因,并希望能解決它?
uj5u.com熱心網友回復:
該測驗僅創建一個用戶實體。對于失敗的觀點來說,這足夠了嗎?
您需要訪問服務器回溯以確切知道出了什么問題。
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:為什么我在運行 LiveServerTestCase 測驗時無法獲得回溯(如果出現錯誤)?更多細節。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/384013.html
上一篇:你能插入一個表,其中一個值是mysql中的宣告變數嗎?[復制]
下一篇:selenium.common.exceptions.InvalidArgumentException:訊息:無效引數:用戶資料目錄已在使用真實Chrome瀏覽器錯誤
