Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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:</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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