如何在Selenium/Django单元测试中创建会话变量? [英] How to Create Session Variables in Selenium/Django Unit Test?

查看:96
本文介绍了如何在Selenium/Django单元测试中创建会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个使用Selenium来测试Django视图的功能测试.当用户进入页面("page2")时,呈现该页面的视图期望找到会话变量"uid"(用户ID).我已经阅读了六篇有关应该如何进行的文章,但没有一篇对我有用.下面的代码显示了Django 文档说应该这样做,但对我也不起作用.当我运行测试时,该视图永远不会完成执行,并且会收到发生服务器错误"消息.有人可以告诉我我在做什么错吗?谢谢.

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)

推荐答案

以下是解决此问题的方法. James Aylett在提到上面的会话ID时暗示了解决方案. jscn展示了如何建立会话,但是他没有提到会话密钥对解决方案的重要性,也没有讨论如何将会话状态链接到Selenium的浏览器对象.

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.

首先,您必须了解,当创建会话键/值对(例如'uid'= 1)时,Django的中间件将在您选择的后端(数据库,文件)中创建会话键/数据/到期日期记录. , 等等.).然后,响应对象会将该会话密钥以Cookie的形式发送回客户端的浏览器.当浏览器发送后续请求时,它将发送回一个包含该密钥的cookie,然后中间件将其用于查找用户的会话项目.

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.

因此,该解决方案要求1.)找到一种方法来获取在创建会话项时生成的会话密钥,然后; 2.)找到一种通过Selenium的Firefox webdriver浏览器对象将该密钥传递回cookie的方法.这是执行此操作的代码:

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)

这篇关于如何在Selenium/Django单元测试中创建会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆