Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9185941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:24:01+00:00 2026-06-17T19:24:01+00:00

I’m trying to write a functional test that uses Selenium to test a Django

  • 0

I’m trying to write a functional test that uses Selenium to test a Django view. When the user comes to a page (“page2”), the view that renders that page expects to find a session variable “uid” (user ID). I’ve read a half dozen articles on how this is supposed to be done but none of them have worked for me. The code below shows how the Django documentation says it should be done but it doesn’t work for me either. When I run the test, the view never completes executing and I get a “server error occurred” message. Could someone please tell me what I’m doing wrong? Thank you.

views.py:

from django.shortcuts import render_to_response

def page2(request):
    uid = request.session['uid']
    return render_to_response('session_tests/page2.html', {'uid': uid})

test.py:

from django.test import LiveServerTestCase
from selenium import webdriver
from django.test.client import Client

class SessionTest(LiveServerTestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(3)
        self.client = Client()
        self.session = self.client.session
        self.session['uid'] = 1

    def tearDown(self):
        self.browser.implicitly_wait(3)
        self.browser.quit()

    def test_session(self):
        self.browser.get(self.live_server_url + '/session_tests/page2/')
        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('Page 2', body.text)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:24:03+00:00Added an answer on June 17, 2026 at 7:24 pm

    Here’s how to solve this problem. James Aylett hinted at the solution when he mentioned the session ID above. jscn showed how to set up a session but he didn’t mention the importance of the session key to a solution and he also didn’t discuss how to link the session state to Selenium’s browser object.

    First, you have to understand that when you create a session key/value pair (e.g. ‘uid’=1), Django’s middleware will create a session key/data/expiration date record in your backend of choice (database, file, etc.). The response object will then send that session key in a cookie back to the client’s browser. When the browser sends a subsequent request, it will send a cookie back that contains that key which is then used by the middleware to lookup the user’s session items.

    Thus, the solution required 1.) finding a way to obtain the session key that is generated when you create a session item and then; 2.) finding a way to pass that key back in a cookie via Selenium’s Firefox webdriver browser object. Here’s the code that does that:

    selenium_test.py:
    -----------------
    
    from django.conf import settings
    from django.test import LiveServerTestCase
    from selenium import webdriver
    from django.test.client import Client
    import pdb
    
    def create_session_store():
        """ Creates a session storage object. """
    
        from django.utils.importlib import import_module
        engine = import_module(settings.SESSION_ENGINE)
        # Implement a database session store object that will contain the session key.
        store = engine.SessionStore()
        store.save()
        return store
    
    class SeleniumTestCase(LiveServerTestCase):
    
        def setUp(self):
            self.browser = webdriver.Firefox()
            self.browser.implicitly_wait(3)
            self.client = Client()
    
        def tearDown(self):
            self.browser.implicitly_wait(3)
            self.browser.quit()
    
        def test_welcome_page(self):
            #pdb.set_trace()
            # Create a session storage object.
            session_store = create_session_store()
            # In pdb, you can do 'session_store.session_key' to view the session key just created.
    
            # Create a session object from the session store object.
            session_items = session_store
    
            # Add a session key/value pair.
            session_items['uid'] = 1
            session_items.save()
    
            # Go to the correct domain.
            self.browser.get(self.live_server_url)
    
            # Add the session key to the cookie that will be sent back to the server.
            self.browser.add_cookie({'name': settings.SESSION_COOKIE_NAME, 'value': session_store.session_key})
            # In pdb, do 'self.browser.get_cookies() to verify that it's there.'
    
            # The client sends a request to the view that's expecting the session item.
            self.browser.get(self.live_server_url + '/signup/')
            body = self.browser.find_element_by_tag_name('body')
            self.assertIn('Welcome', body.text)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.