所以我有一个selenium功能测试套件.我已经在几个测试中测试了登录/注册功能,方法是将Selenium客户机导航到注册页面,输入用户名和密码,然后告诉Selenium使用相同的凭据登录.现在,我想测试站点的"需要登录"区域的其他部分,而不必告诉Selenium单击并在测试浏览器中输入文本.

换句话说,我想使用这样的东西(我在我的视图单元测试中使用得很好):

self.client = Client()  
self.user = User.objects.create_user('temporary', 'temporary@gmail.com', 'temporary')  
self.user.save()  
self.client.login(username='temporary', password='temporary')

在我的Selenium测试中,所以我不必在每个测试中重复冗长的手动登录过程(因为我已经在前面的测试中测试了登录系统)

到目前为止,我只是为我需要登录的每个测试复制并粘贴了"登录流"Selenium指令.这会导致我的测试每次额外花费5-6秒的时间,这使得我的function_tests.py文件非常臃肿.

我所有的谷歌搜索都让我进入了教我如何使用Selenium测试登录的页面.

提前谢谢您.

推荐答案

您无法从selenium驱动程序登录用户.没有黑客是不可能的.

但是您可以通过将每个TestCase移动到Setup方法来登录一次.

您还可以通过创建从LiveServerTestCase继承的类来避免复制粘贴.

UPDATE

此代码适用于我:

self.client.login(username=superuser.username, password='superpassword') #Native django test client
cookie = self.client.cookies['sessionid']
self.browser.get(self.live_server_url + '/admin/')  #selenium will set cookie domain based on current page domain
self.browser.add_cookie({'name': 'sessionid', 'value': cookie.value, 'secure': False, 'path': '/'})
self.browser.refresh() #need to update page for logged in user
self.browser.get(self.live_server_url + '/admin/')

Django相关问答推荐

使用FormWizard将信息从视图传递到表单

我希望用户能够预订相同的桌子,但不是在同一时间

社工/社工简戈

Django中的DateTimeField到DateField

Django中每个类型/代理的最新行

Django: 无法将我的 comments 关联到特定产品

未为部署的 django rest 框架加载静态文件

Django InlineModelAdmin:部分显示内联模型并链接到完整模型

验证 Django 模型对象的正确方法?

django- nginx: [emerg] open() "/etc/nginx/proxy_params" 在 /etc/nginx/sites-enabled/myproject:11 中失败(2:没有这样的文件或目录)

Django模型字段按变量

在 Django 1.8 或更高版本中填充时出现Models aren't loaded yet"错误

单击弹出框会滚动回页面顶部 [Bootstrap 和 Django]

Django查询否定

如何在 Django 中执行批量插入?

Django Rest 框架 ImageField

Django:按位置排序,忽略 NULL

Django - 保存新对象时如何获取 self.id?

Numpy 数组到 base64 并返回到 Numpy 数组 - Python

左加入 Django ORM