Search code examples
pythondjangoselenium-webdriverpython-unittest

Errors testing in Django with Selenium


When I pass my test in Django the result shows an output errors that looks like that:

Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/Users/bav/venv/django/lib/python3.9/site-packages/django/test/testcases.py", line 1723, in __call__
    return super().__call__(environ, start_response)
  File "/Users/bav/venv/django/lib/python3.9/site-packages/django/core/handlers/wsgi.py", line 124, in __call__
    response = self.get_response(request)
  File "/Users/bav/venv/django/lib/python3.9/site-packages/django/test/testcases.py", line 1706, in get_response
    return self.serve(request)
  File "/Users/bav/venv/django/lib/python3.9/site-packages/django/test/testcases.py", line 1718, in serve
    return serve(request, final_rel_path, document_root=self.get_base_dir())
  File "/Users/bav/venv/django/lib/python3.9/site-packages/django/views/static.py", line 34, in serve
    fullpath = Path(safe_join(document_root, path))
  File "/Users/bav/venv/django/lib/python3.9/site-packages/django/utils/_os.py", line 17, in safe_join
    final_path = abspath(join(base, *paths))
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/posixpath.py", line 76, in join
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not NoneType

The code I am using for the test is:

class test_incluir_elemento(LiveServerTestCase):

def setUp(self):
    self.user = User.objects.create_user(username='testuser', password='secretpassword')
    self.driver = webdriver.Chrome()
    self.driver.implicitly_wait(10)

def tearDown(self):
    self.driver.close()

def test_incluir_elemento(self):
    self.driver.get(self.live_server_url)
    login = self.driver.find_element("id", "usuario")
    contrasena = self.driver.find_element("id", "contrasena")
    boton_login = self.driver.find_element("id", "boton-registro")
    login.send_keys("testuser")
    contrasena.send_keys("secretpassword")
    boton_login.click()
    modificar = self.driver.find_element("id", "modificar")
    modificar.click()

The final output says that all the test are OK, so I assume that my test is working properly but these errors annoy me and I don't find information about it.


Solution

  • The solution that I found to the problem is use StaticLiveServerTestCase in the class statement instead of LiveServerTestCase. Doing some research seems to be a problem with the charge of static files but I am not sure about that.