Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Create Session Variables in Selenium/Django Unit Test?
    primarykey
    data
    text
    <p>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 <a href="https://docs.djangoproject.com/en/dev/topics/testing/overview/#django.test.client.Client.session" rel="nofollow">documentation</a> 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.</p> <p>views.py:</p> <pre><code>from django.shortcuts import render_to_response def page2(request): uid = request.session['uid'] return render_to_response('session_tests/page2.html', {'uid': uid}) </code></pre> <p>test.py:</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload