Testing Django Admin with Selenium

268 views
Skip to first unread message

Derek

unread,
Apr 16, 2017, 7:39:01 AM4/16/17
to django-users
Python 3.5; Django 1.10.7

I am struggling to get access to the admin pages via Selenium.  

I assume the issue is with the user login not being set properly - I don't want to go via the login page/form every time, so was attempting the session approach.  Below is the code that shows the problem.  The last few lines are attempting to access a known element (appears on any admin page); but the actual page that gets displayed is one you would see if you were not logged in.

Any hints on how to make this work would be appreciated. 

# -*- coding: utf-8 -*-
"""
Purpose: Test admin list display
"""
# lib
import os
# third party
from selenium import webdriver
# django
from django.conf import settings
from django.test import LiveServerTestCase
from django.contrib.auth import get_user_model, SESSION_KEY, \
    BACKEND_SESSION_KEY, HASH_SESSION_KEY
from django.contrib.sessions.backends.db import SessionStore
# project
from users.models import CustomUser as User

os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = 'localhost:8007'


class LoginSeleniumTest(LiveServerTestCase):
    """selenium-based test"""

    def setUp(self):
        # setup browser
        if settings.USE_CHROME:
            self.browser = webdriver.Chrome(settings.CHROME_DRIVER)
        elif settings.USE_FF:
            self.browser = webdriver.Firefox()
        else:
            raise NotImplementedError('Unknown browser for Selenium tests')
        # create user
        self.email = 'f...@test.com'
        user = User(
            is_staff=True, is_active=True, is_superuser=True,
            email=self.email, full_name="Foo Bar", nick_name="Foo")
        user.set_password('foobar')
        user.save()
        super(LoginSeleniumTest, self).setUp()

    def test_login_and_access_admin(self):
        # create fake session with real user
        user = get_user_model().objects.get(email=self.email)
        self.session = SessionStore()
        self.session[SESSION_KEY] = user.pk
        self.session[BACKEND_SESSION_KEY] = 'django.contrib.auth.backends.ModelBackend'
        self.session[HASH_SESSION_KEY] = user.get_session_auth_hash()
        self.session.save()
        print("HASH_KEY:\n", self.session[HASH_SESSION_KEY])
        # set cookie based on current page domain
        self.browser.get(self.live_server_url + '/dummy/')
        self.browser.add_cookie(
            {'name': settings.SESSION_COOKIE_NAME,
             'value': self.session.session_key,
             'secure': False,
             'path': '/'}
        )
        # access an admin (password-protected) page
        page_name = '/admin/'
        print("URL:\n", self.live_server_url + page_name)
        self.browser.get(self.live_server_url + page_name)
        print("PAGE:\n", self.browser.page_source)
        # throws a selenium.common.exceptions.NoSuchElementException ...?
        result = self.browser.find_element_by_id('user-tools')

Reply all
Reply to author
Forward
0 new messages