Selenium gives Server Error 500 after click (Django, pytest)

CodingCat

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?

Dušan Maďar

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

After hosting to server symfony gives 500 internal server error

Django, nginx, gunicorn: why on some page gives Server Error (500)?

Whenever debug=False in django, Heroku gives Server Error (500) and when debug=True no error

Creating symlink gives 500 Internal Server Error

PHP MySql insert gives server error 500

500 - Internal server error after publish on server

Selenium / Django gives Foreign Key error

Server Error (500) Django App on AWS server

How to fix an error in a php variable? Server gives 500 error

Jupyer lab gives Error 500: Internal Server Error

Ajax call to webmethod gives ERROR 500 Internal server error

simple AJAX JQuery example gives me 500 internal server error

Server gives 500 code error but still serves content

Excel/PowerBI OData import gives internal server error 500

ExpressJS API Deployed on Heroku Gives 500 Internal Server Error

Htaccess rewrite url gives 500 Internal server error

Apache server mod_wsgi in Redhat gives 500 error

Internal server error 500 after deleting wwwroot

Django gives NoReverseMatch error after changes to urlpattern

Django Google App Engine Server Error 500

Server Error (500) in accessing a section in Django app

Bitnami Django Stack 500 Internal Server Error

Django not sending Server Error (500) emails

Server error (500) Django deployment on heroku

500 (Internal Server Error) AJAX Django

500 Internal Server Error in production - Django

Django showing 500 Internal Server Error on HTTPS

I cant click on an element with selenium and it gives me the error element not interactable

Pytest, selenium, fixture throws an error after the first test InvalidSessionIdException

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive