Login with code when using LiveServerTestCase with Django

前端 未结 3 1562
名媛妹妹
名媛妹妹 2021-01-30 13:17

So I have a Selenium functional test suite. I\'ve already tested login/signup functionality in a few tests by navigating the Selenium client to the signup page, entering in a us

相关标签:
3条回答
  • 2021-01-30 13:57

    There is a library available on GitHub for this purpose: django-selenium-login

    0 讨论(0)
  • 2021-01-30 14:00

    You can't login user from selenium driver. It's just impossible without some hacks.

    But you can login once per TestCase by moving it to setUp method.

    You can also avoid copy-pasting by creating your class inherit from LiveServerTestCase.

    UPDATE

    This code worked for me:

    self.client.login(username=superuser.username, password='superpassword') #Native django test client
    cookie = self.client.cookies['sessionid']
    self.browser.get(self.live_server_url + '/admin/')  #selenium will set cookie domain based on current page domain
    self.browser.add_cookie({'name': 'sessionid', 'value': cookie.value, 'secure': False, 'path': '/'})
    self.browser.refresh() #need to update page for logged in user
    self.browser.get(self.live_server_url + '/admin/')
    
    0 讨论(0)
  • 2021-01-30 14:05

    In Django 1.8 it is possible to create a pre-authenticated session cookie and pass it to Selenium.

    In order to do this, you'll have to:

    1. Create a new session in your backend;
    2. Generate a cookie with that newly created session data;
    3. Pass that cookie to your Selenium webdriver.

    The session and cookie creation logic goes like this:

    # create_session_cookie.py
    from django.conf import settings
    from django.contrib.auth import (
        SESSION_KEY, BACKEND_SESSION_KEY, HASH_SESSION_KEY,
        get_user_model
    )
    from django.contrib.sessions.backends.db import SessionStore
    
    def create_session_cookie(username, password):
    
        # First, create a new test user
        user = get_user_model()
        user.objects.create_user(username=username, password=password)
    
        # Then create the authenticated session using the new user credentials
        session = SessionStore()
        session[SESSION_KEY] = user.pk
        session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
        session[HASH_SESSION_KEY] = user.get_session_auth_hash()
        session.save()
    
        # Finally, create the cookie dictionary
        cookie = {
            'name': settings.SESSION_COOKIE_NAME,
            'value': session.session_key,
            'secure': False,
            'path': '/',
        }
        return cookie
    

    Now, inside your Selenium tests:

    #selenium_tests.py
    
    # assuming self.webdriver is the selenium.webdriver obj.
    from create_session_cookie import create_session_cookie
    
    session_cookie = create_session_cookie(
        username='test@email.com', password='top_secret'
    )
    
    # visit some url in your domain to setup Selenium.
    # (404 pages load the quickest)
    self.driver.get('your-url' + '/404-non-existent/')
    
    # add the newly created session cookie to selenium webdriver.
    self.driver.add_cookie(session_cookie)
    
    # refresh to exchange cookies with the server.
    self.driver.refresh()
    
    # This time user should present as logged in.
    self.driver.get('your-url')
    
    0 讨论(0)
提交回复
热议问题