Issue
I have a Django app that I want to test via pytest and Selenium.
The routine I'm trying to go through is to let Selenium log in, then go to the menu, choose one of the options. This redirects to a new page, which is found fine. There, I let selenium enter data and click a button to start a submission. This, too, works, and redirects to a third page.
On this third "your submission was created successfully" page, there is a link displayed that the user can click to collect the results of their submission (this page . This link is displayed correctly, the href URL is fine. But when I let Selenium click on it, I suddenly get a Server Error 500
:
<html lang="en"><head>
<title>Server Error (500)</title>
</head>
<body>
<h1>Server Error (500)</h1><p></p>
</body></html>
When I do the exact same thing manually, it works fine.
Here's my test code (slightly simplified):
@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)
I have already tried to use WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "click here for your results"))).click()
, and that doesn't seem to even click.
I know that error 500 mean's the error is on the server side. But before clicking, the server is fine. All the redirections before this step work fine. The URLs also check out. And manually, everything works as expected. I have no idea how to narrow down the problem further.
How can I investigate the reason for this error, and hopefully fix it?
Solution
The test creates only a user instance. Is that enough for the view which fails?
You need to get to server traceback to know for sure what's wrong.
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):
...
Take a look at Django: why i can't get the tracebacks (in case of error) when i run LiveServerTestCase tests? for more details.
Answered By - Dušan Maďar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.