I've been working in a Django project for a while and am starting on a new app, trying to do more automated testing with Selenium at the same time. I'm using http://www.tdd-django-tutorial.com/ as a guide.
I'm trying to test the ability to log in to my application. My test can pull up the page and fill in text fields without trouble, but when it clicks on the submit button, it hangs - Firefox keeps trying to load the new page but it never happens. It looks like deadlock to me, but I don't understand it well enough to know what's going on.
Other details: I'm using Django's built-in login view. I have another test that successfully logs in to the admin site. I can log in to my application just fine when I test manually. The application accesses a remote MySQL database.
Here's my test:
from django.test import TestCase, LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
class BucloudTest(LiveServerTestCase):
"""Tests shared functionality (login, network and app selection)."""
fixtures = ['24aug2012_dev_auth.json']
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(5)
def tearDown(self):
self.browser.quit()
def test_good_login(self):
"""Tests that a user can log in using valid credentials."""
self.browser.get(self.live_server_url + "/login/")
user_css = "[placeholder=Username]"
user_field = self.browser.find_element_by_css_selector(user_css)
user_field.send_keys("test_user1")
pw_css = "[placeholder=Password]"
pw_field = self.browser.find_element_by_css_selector(pw_css)
pw_field.send_keys("test")
button = self.browser.find_element_by_css_selector("[value='Sign in']")
button.click()
WebDriverWait(self.browser, 30).until(
lambda driver: driver.find_element_by_tag_name('body'))
body = self.browser.find_element_by_tag_name("body")
self.assertIn("Properties", body.text)
print "ran tests YAY!!"
I run the test with manage.py test functests --liveserver=localhost:8080-8090.
Thanks very much for any suggestions!
It might be due to the fact that the content of your test_good_login() function is not indented
Related
I'm testing my Django application with Selenium in Docker. I encounter a peculiar thing related to cookies availability (I use cookies to authenticate in my tests).
Here is the code that works:
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from users.models import CustomUser
class SomeTest(StaticLiveServerTestCase):
#classmethod
def setUpClass(cls):
cls.host = "web" # Docker service name
super().setUpClass()
CustomUser.objects.create_user(username="user", password="password")
def setUp(self):
self.browser = webdriver.Remote("http://selenium:4444/wd/hub", DesiredCapabilities.FIREFOX)
def tearDown(self):
self.browser.quit()
def test2(self):
self.client.login(username="user", password="password")
cookie = self.client.cookies["sessionid"]
...
However, when I insert there another test case before test2, let it be something as simple as
def test1(self):
pass
then the code crashes with the following error:
Traceback (most recent call last):
File "/home/mysite/functional_tests/test.py", line 28, in test2
cookie = self.client.cookies["sessionid"]
KeyError: 'sessionid'
So the only difference between the working and not-working code is a dummy test function, but what does it change? As far as I know the setUp and tearDown methods make sure that the "environment" is the same for every test case, no matter what happens in other test methods and here it clearly depends on the (non-)existence of other test cases before running my test... Is there something I misunderstand? Or is it some kind of a bug?
Any help will be appreciated.
My setup:
Django==2.2.5
selenium==3.141.0
Docker version - 19.03.5
I've solved it and I'm posting the answer here in case anyone else encounters similar issues.
So the problem here was not with test case order, Docker, Selenium, or anything within the code itself but with my lack of understanding of how class StaticLiveServerTestCase behaves. Namely, this class inherits from LiveServerTestCase which in turn inherits from TransactionTestCase which tears down the database after each test case (and sets it up before another test case) - more on this can be found in Django docs. And as I was creating the user in setUpClass - which is run once per all the test cases in the class - it was indeed created but removed (together with the whole database) after any first test case. Sowhen I was doing self.client.login(username="user", password="password") it was not a problem with cookies or authentication per se but with the fact that the user simply didn't exist.
:-)
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 your class inherit 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/')
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:
Create a new session in your backend;
Generate a cookie with that newly created session data;
Pass that cookie to your Selenium webdriver.
The session and cookie creation logic goes like this:
# create_session_cookie.py
from django.conf import settings
from django.contrib.auth import (
SESSION_KEY, BACKEND_SESSION_KEY, HASH_SESSION_KEY,
get_user_model
)
from django.contrib.sessions.backends.db import SessionStore
def create_session_cookie(username, password):
# First, create a new test user
user = get_user_model()
user.objects.create_user(username=username, password=password)
# Then create the authenticated session using the new user credentials
session = SessionStore()
session[SESSION_KEY] = user.pk
session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
session[HASH_SESSION_KEY] = user.get_session_auth_hash()
session.save()
# Finally, create the cookie dictionary
cookie = {
'name': settings.SESSION_COOKIE_NAME,
'value': session.session_key,
'secure': False,
'path': '/',
}
return cookie
Now, inside your Selenium tests:
#selenium_tests.py
# assuming self.webdriver is the selenium.webdriver obj.
from create_session_cookie import create_session_cookie
session_cookie = create_session_cookie(
username='test#email.com', password='top_secret'
)
# visit some url in your domain to setup Selenium.
# (404 pages load the quickest)
self.driver.get('your-url' + '/404-non-existent/')
# add the newly created session cookie to selenium webdriver.
self.driver.add_cookie(session_cookie)
# refresh to exchange cookies with the server.
self.driver.refresh()
# This time user should present as logged in.
self.driver.get('your-url')
There is a library available on GitHub for this purpose: django-selenium-login
I am creating Selenium tests for my App.
I can create a new user, but I can't seem to figure out how to have it deleted from the database.
After the tests run successfully the first time, subsequent tests fail because the username already exists.
Why am I not able to query the newly created record in the debugger despite being able to see the new record on the page?
How do I delete a record from the database in a test?
This is what I have been doing:
from selenium import webdriver
from django.utils import unittest
from forum.models import Question, Answer, User
class TestOSQAAuthentication(unittest.TestCase):
scheme = 'http'
host = 'localhost'
port = '4444'
def setUp(self):
self._driver = webdriver.Firefox()
self._driver.implicitly_wait(25)
def test_anon_can_create_new_account_manually(self):
self._driver.get('http://localhost:8000/account/local/register/')
self._driver.find_element_by_id('id_username').send_keys('MrManual')
self._driver.find_element_by_id('id_email').send_keys('test#gmail.com')
self._driver.find_element_by_id('id_password1').send_keys('test')
self._driver.find_element_by_id('id_password2').send_keys('test')
self._driver.find_element_by_id('bnewaccount').click()
# verify MrManual was created
self._driver.get('http://localhost:8000/users/')
self._driver.find_element_by_link_text('MrManual')
# MrManual seems to be created, but I don't see MrManual in the database during debugging with:
# import ipdb; ipdb.set_trace()
#ipdb> User.objects.all()
#[<User: Bryan>, <User: Kallie>, <User: Stalin>]
# here I am trying to delete the user from the database directly.
User.objects.filter(username="MrManual").delete()
"""For some reason I can't delete the record from the database from the test.
Selenium can find the new user in the browser, but I can't query the database to find it."""
If you use Selenium, instead of django.utils.unittest.TestCase please use django.test.TransactionalTestCase or even better, LiveServerTestCase.
I have some external services. My Django app is built on top of my external service APIs. In order to talk to my external service, I have to pass in an auth cookies, which I can get by reading User (that cookie != django cookies).
Using test tools like webtests, requests, I have trouble writing my tests.
class MyTestCase(WebTest):
def test_my_view(self):
#client = Client()
#response = client.get(reverse('create')).form
form = self.app.get(reverse('create'), user='dummy').form
print form.fields.values()
form['name'] = 'omghell0'
print form
response = form.submit()
I need to submit a form, which creates, say, a user on my external service. But to do that, I normally would pass in request.user (in order to authenticate my privilege to external service). But I don't have request.user.
What options do I have for this kind of stuff?
Thanks...
Suppose this is my tests.py
import unittest
from django.test.client import Client
from django.core.urlresolvers import reverse
from django_webtest import WebTest
from django.contrib.auth.models import User
class SimpleTest(unittest.TestCase):
def setUp(self):
self.usr = User.objects.get(username='dummy')
print self.usr
.......
I get
Traceback (most recent call last):
File "/var/lib/graphyte-webclient/webclient/apps/codebundles/tests.py", line 10, in setUp
self.usr = User.objects.get(username='dummy')
File "/var/lib/graphyte-webclient/graphyte-webenv/lib/python2.6/site-packages/django/db/models/manager.py", line 132, in get
return self.get_query_set().get(*args, **kwargs)
File "/var/lib/graphyte-webclient/graphyte-webenv/lib/python2.6/site-packages/django/db/models/query.py", line 341, in get
% self.model._meta.object_name)
DoesNotExist: User matching query does not exist
But if I test the User.objects in views, I am okay.
You need to use the setUp() method to create test users for testing - testing never uses live data, but creates a temporary test database to run your unit tests. Read this for more information: https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#writing-unit-tests
EDIT:
Here's an example:
from django.utils import unittest
from django.contrib.auth.models import User
from myapp.models import ThisModel, ThatModel
class ModelTest(unittest.TestCase):
def setUp(self):
# Create some users
self.user_1 = User.objects.create_user('Chevy Chase', 'chevy#chase.com', 'chevyspassword')
self.user_2 = User.objects.create_user('Jim Carrey', 'jim#carrey.com', 'jimspassword')
self.user_3 = User.objects.create_user('Dennis Leary', 'dennis#leary.com', 'denisspassword')
Also note that, if you are going to use more than one method to test different functionality, you should use the tearDown method to destroy objects before reinstantiating them for the next test. This is something that took me a while to finally figure out, so I'll save you the trouble.
def tearDown(self):
# Clean up after each test
self.user_1.delete()
self.user_2.delete()
self.user_3.delete()
Django recommends using either unit tests or doc tests, as described here. You can put these tests into tests.py in each apps directory, and they will run when the command `python manage.py test" is used.
Django provides very helpful classes and functions for unit testing, as described here. In particular, the class django.test.Client is very convenient, and lets you control things like users.
https://docs.djangoproject.com/en/1.4/topics/testing/#module-django.test.client
Use the django test client to simulate requests. If you need to test the behavior of the returned result then use Selenium.
I like to run tests before I commit.
I'm new to Selenium and I don't understand how to run the tests and not change the database.
My local database has dozens of identical posted questions.
Is there any way that I can have have these tests run and not have the database restored to it's original state on tearDown?
from selenium import webdriver
from django.utils import unittest
from selenium.webdriver.support.ui import WebDriverWait
class TestAuthentication(unittest.TestCase):
scheme = 'http'
host = 'localhost'
port = '4444'
def setUp(self):
self._driver = webdriver.Firefox()
self._driver.implicitly_wait(5)
def login_as_Bryan(self):
self._driver.get('http://localhost:8000/account/signin/')
user = self._driver.find_element_by_id('id_username')
user.send_keys("Bryan")
password = self._driver.find_element_by_id('id_password')
password.send_keys('***************')
submit = self._driver.find_element_by_id('blogin')
submit.click()
def test_user_should_be_able_to_login_manually(self):
self.login_as_Bryan(self)
message = self._driver.find_element_by_class_name('darkred')
self.assertEqual("Welcome back Bryan, you are now logged in", message.text)
def test_Bryan_can_post_question(self):
self.login_as_Bryan()
self._driver.find_element_by_link_text("ask a question").click()
self._driver.find_element_by_id('id_title').send_keys("Question should succeed")
self._driver.find_element_by_id('editor').send_keys("This is the body text.")
self._driver.find_element_by_id('id_tags').send_keys("test")
self._driver.find_element_by_class_name("submit").click()
self.assertTrue(self._driver.find_element_by_link_text("Question should succeed"))
def tearDown(self):
self._driver.quit()
The issue is not so much Selenium as it is your execution environment. It depends on how you fire up your application.
In general, you need to bootstrap your application launch so that it points to a temporary database for use only during that test. After the test execution, you should delete that database.
Alternately, you can provide a UI mechanism in your actual website to clear / refresh the test database. In that case, you still need a test database, but you don't need to delete/recreate it with every test execution.
You can use django-selenium, it runs tests on test database