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.
In Django 1.8 it is possible to create a pre-authenticated session cookie and pass it to Selenium.
In order to do this, you'll have to:
The session and cookie creation logic goes like this:
Now, inside your Selenium tests:
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: