Search code examples
pythondjangoseleniumsession-cookiesdjango-sessions

How to prevent Django from overriding sessionid cookie?


I'm trying to have 2 browser windows with the same session in a test. Note that each browser has a different firefox profile because of selenium limitations.

The test (you can try it, as long as you have a test/test staff user) is:

import time

from django.test import LiveServerTestCase
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException

class ScriptTestCase(LiveServerTestCase):
    def do_admin_login(self, username, password, browser):
        browser.get('%s%s' % (self.live_server_url, '/admin/'))
        username_input = browser.find_element_by_name("username")
        username_input.send_keys(username)
        password_input = browser.find_element_by_name("password")
        password_input.send_keys(password)
        browser.find_element_by_xpath('//input[@value="Log in"]').click()

    def test_double_window_inactivity(self):
        browser0 = WebDriver()
        self.do_admin_login('test', 'test', browser0)
        cookie = browser0.get_cookie('sessionid')
        cookies = {'name': 'sessionid', 'value': cookie['value']}

        browser1 = WebDriver()
        browser1.add_cookie(cookies)
        print 1, browser0.get_cookie('sessionid')['value']
        print 2, browser1.get_cookie('sessionid')['value']
        browser1.get('%s%s' % (self.live_server_url, '/admin/'))
        print 3, browser0.get_cookie('sessionid')['value']
        print 4, browser1.get_cookie('sessionid')['value']

The output is:

1 08ba4efecf00d2b98aacd174ed20e144
2 08ba4efecf00d2b98aacd174ed20e144
3 08ba4efecf00d2b98aacd174ed20e144
4 cb49379190cb37735697c5c82ab300fc

As you can see, after browser1 opened /admin/, its sessionid was overridden unexpectively.

How to work around this ? I'm trying to have browser1 to have the same session as browser0.

Fixed code, thanks to Hedde ---

  def test_double_window_inactivity(self):
      self.browser.execute_script('window.open("/admin/", "other")')

      for win in self.browser.window_handles:
          self.browser.switch_to_window(win)
          self.assertWarningHidden()

      time.sleep(5+1)  # Added one second to compensate for fadeIn
      for win in self.browser.window_handles:
          self.browser.switch_to_window(win)
          self.assertWarningShown()

      time.sleep(5+1)  # Added one second to compensate for lag
      for win in self.browser.window_handles:
          self.browser.switch_to_window(win)
          self.assertWarningNotInPage()

Solution

  • I believe things are working just like they are supposed to. That is, sessions should be shared across tabs and/or windows, not across browsers.

    Looking at your code it seems you are not intending to share sessions across browsers, just across two firefox windows. Instantiating two WebDriver classes is not the way you ought to be working. Have a look at the selenium source code, firefox.WebDriver inherits from remote.WebDriver, which has methods to switch tabs and/or windows (e.g. line 490).

    You should set the browser as a class variable and open windows or tabs from it accordingly.