Django项目中使用的Selenium测试的快速登录
django-selenium-login的Python项目详细描述
django selenium login为selenium驱动程序设置会话cookie,并为django后端创建会话对象,以强制用户快速登录。
用法
使用django selenium登录,您可以使用force_login函数强制用户快速登录,然后使用selenium驱动程序在需要登录的页面上继续。该函数接受django用户、selenium驱动程序和live服务器的基本url。下面是如何在测试中使用force\u login函数的示例:
fromseleniumloginimportforce_logindeftest_use_django_selenium_login_to_force_login(selenium,live_server):User=get_user_model()user=User.objects.create_user(username='myuser',password='password')force_login(user,selenium,live_server.url)selenium.get('{}/a/url/which/requires/login/'.format(live_server.url))
在为selenium驱动程序设置会话cookie之前,驱动程序必须访问项目中的页面。默认情况下,它将尝试访问/页面404/页面。可以通过更改测试设置文件中的SELENIUM登录起始页设置来更改此设置此库中的测试使用空白页
force_login函数指定true作为set_domain_session_cookie的默认值。如果设置为false,则不会为cookie设置域。当使用localhost启动liveserver时,在firefox中使用force-login时,这非常有用。由于某些原因,firefox的会话cookie中不接受没有点的域名。
安装
使用pip安装django selenium登录:
pip install django-selenium-login
对于开发人员
使用:
make venv
使用tox:
tox -- --driver=Chrome
使用环境变量会话引擎更改身份验证后端。
对chrome、firefox和phantomjs的所有身份验证后端运行测试:
./run_tests.sh