Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

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 username and password, and then telling Selenium to login with those same credentials. Now I want to test other parts of the "login required" areas of the site without having to tell Selenium to click and enter text into the test browser.

In other words, I would like to use something like this (which I use just fine in my view unit tests):

self.client = Client()  

self.user = User.objects.create_user('temporary', '[email protected]', 'temporary')  

self.client.login(username='temporary', password='temporary')

in my Selenium tests so I don't have to repeat the lengthy manual login process in every one of my tests (since I've already tested the login system in earlier tests as I said before)

As of right now, I just copy and paste the 'login flow' Selenium instructions for each of my tests that require login. This causes my tests to take an addition 5-6 seconds each and it makes my file very bloated.

All my Googling has brought me to pages teaching me how to test login with Selenium.

Thanks in advance.

1 Answer

0 votes
by (62.9k points)

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

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

  1. Create a new session in your backend;

  2. Produce a cookie with that newly created session data;

  3. Pass that cookie to your Selenium webdriver.

The logic of session and cookie creation is shown below:


from django.conf import settings

from django.contrib.auth import (




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 using the new user credentials,  create the authenticated session

    session = SessionStore()

    session[SESSION_KEY] =


    session[HASH_SESSION_KEY] = user.get_session_auth_hash()


    # 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:

# assuming self.webdriver is the selenium.webdriver obj.

from create_session_cookie import create_session_cookie


session_cookie = create_session_cookie(

    email='[email protected]', password='top_secret'



#  to setup Selenium, visit some url in your domain.

# (404 pages load the quickest)

self.driver.get('your-url' + '/404-non-existent/')


# add the newly created session cookie to selenium webdriver.



# refresh to exchange cookies with the server.



# This time users should present as logged in.


I hope it helps!

Browse Categories