使用Django使用LiveServerTestCase时,使用代码登录 [英] Login with code when using LiveServerTestCase with Django

查看:190
本文介绍了使用Django使用LiveServerTestCase时,使用代码登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  self.client = Client()
self.user = User.objects.create_user('temporary','temporary@gmail.com','temporary')
self.user.save()
self。在我的Selenium测试中,client.login(username ='temporary',password ='temporary')

我不必在每次测试中重复漫长的手动登录过程(因为我之前已经在早期测试中测试过登录系统)



到目前为止,我只需要复制并粘贴需要登录的每个测试的login flowSelenium指令。这使我的测试每次增加5-6秒,这使得我的function_tests.py文件变得非常blo肿。



我的所有Google搜索引用了我,教我如何使用Selenium来测试登录。



提前感谢

解决方案

您不能从硒鼓驱动程序登录用户。不过没有一些黑客是不可能的。



但是,您可以通过将其移动到setUp方法登录一次TestCase。



您还可以通过创建LiveServerTestCase中的类来避免复制粘贴。



更新



此代码适用于我:

  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将设置基于cookie的域在当前页面域
self.browser.add_cookie({'name':'sessionid','value':cookie.value,'secure':False,'path':'/'})
self.browser.refresh()#need更新登录用户页面
self.browser.get(self.live_server_url +'/ admin /')


So I have a Selenium functional test suite. I've already tested login/signup functionality in a few tests by navigating the Selenium client to the signup page, entering in a username and password, and then telling Selenium to login with those same credentials. Now I want to test other parts of the "login required" areas of the site without having to tell Selenium to click and enter text into the test browser.

In other words, I would like to use something like this (which I use just fine in my view unit tests):

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

in my Selenium tests so I don't have to repeat the lengthy manual login process in every one of my tests (since I've already tested the login system in earlier tests as I said before)

As of right now, I just copy and paste the 'login flow' Selenium instructions for each of my tests that require login. This causes my tests to take an addition 5-6 seconds each and it makes my function_tests.py file very bloated.

All my Googling has brought me to pages teaching me how to test login with Selenium.

Thanks in advance.

解决方案

You can't login user from selenium driver. It's just impossible without some hacks.

But you can login once per TestCase by moving it to setUp method.

You can also avoid copy-pasting by creating you class inhereted from LiveServerTestCase.

UPDATE

This code worked for me:

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使用LiveServerTestCase时,使用代码登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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