Skip to content

Commit 64def85

Browse files
committed
Introducing integration tests
$ TEST_BROWSER=Chrome python redakcja/manage.py test --settings settings.selenium tests/integration/
1 parent ff57369 commit 64def85

File tree

6 files changed

+159
-1
lines changed

6 files changed

+159
-1
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@ nbproject/*
3030
.pydevproject
3131
.settings
3232

33-
node_modules
33+
node_modules
34+
35+
/static_test
36+
chromedriver.log

redakcja/settings/integration_test.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from redakcja.settings.test import *
2+
3+
NOSE_ARGS = ()
4+
5+
STATIC_ROOT_SYMLINK = os.path.dirname(STATIC_ROOT) + '_test'
6+
STATICFILES_DIRS.append(STATIC_ROOT_SYMLINK)
7+

tests/__init__.py

Whitespace-only changes.

tests/integration/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
from django.conf import settings
3+
4+
if not os.path.exists(settings.STATIC_ROOT_SYMLINK):
5+
os.symlink(settings.STATIC_ROOT, settings.STATIC_ROOT_SYMLINK)

tests/integration/base.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import os
2+
import inspect
3+
from urlparse import urlparse
4+
5+
from django.test import LiveServerTestCase
6+
from django.test.client import Client
7+
from django.conf import settings
8+
from django.contrib.auth.models import User, Permission
9+
from django.utils.translation import ugettext as _
10+
11+
from selenium import webdriver, selenium
12+
from selenium.webdriver.support.wait import WebDriverWait
13+
14+
15+
class SeleniumTestCase(LiveServerTestCase):
16+
17+
@classmethod
18+
def setUpClass(cls):
19+
LiveServerTestCase.setUpClass()
20+
cls.browser = getattr(webdriver, os.environ.get('TEST_BROWSER', 'Firefox'))()
21+
cls.browser.implicitly_wait(5)
22+
23+
@classmethod
24+
def tearDownClass(cls):
25+
LiveServerTestCase.tearDownClass()
26+
cls.browser.quit()
27+
28+
def setUp(self):
29+
self.browser.delete_all_cookies()
30+
31+
def create_user(self, username = 'testuser', passwd = 'passwd', do_login = False):
32+
user = User.objects.create_user(username, '', passwd)
33+
user._plain_passwd = passwd
34+
if do_login:
35+
self.login_user(user)
36+
return user
37+
38+
def create_super_user(self, *args, **kwargs):
39+
user = self.create_user(*args, **kwargs)
40+
user.is_superuser = True
41+
user.save()
42+
return user
43+
44+
def login_user(self, user):
45+
client = Client()
46+
client.login(username = user.username, password = user._plain_passwd)
47+
48+
if not self.browser.current_url.startswith(self.live_server_url):
49+
self.browser.get(self.live_server_url+'/not_existing_url')
50+
51+
self.browser.find_element_by_tag_name('body') # Make sure the page is actually loaded before setting the cookie
52+
self.browser.delete_cookie(settings.SESSION_COOKIE_NAME)
53+
self.browser.add_cookie(dict(name = settings.SESSION_COOKIE_NAME,
54+
value = client.cookies[settings.SESSION_COOKIE_NAME].value,
55+
path = '/')
56+
)
57+
58+
def get_main_page(self):
59+
self.browser.get(self.live_server_url)
60+
self.browser.find_element_by_tag_name('body')
61+
return MainPage(self.browser)
62+
63+
64+
class Page(object):
65+
def __init__(self, browser):
66+
self.browser = browser
67+
68+
69+
class MainPage(Page):
70+
71+
def __init__(self, browser):
72+
Page.__init__(self, browser)
73+
self.tab = None
74+
75+
@property
76+
def element(self):
77+
return self.browser.find_element_by_tag_name('body')
78+
79+
def select_tab(self, tab_title):
80+
for a in self.element.find_element_by_id('tabs-nav-left').find_elements_by_tag_name('a'):
81+
if a.text == tab_title:
82+
a.click()
83+
self.tab = find_tab_class(tab_title)(self.browser)
84+
return
85+
raise Exception, 'Tab not found'
86+
87+
88+
def find_tab_class(tab_title):
89+
for obj in globals().values():
90+
if inspect.isclass(obj) and issubclass(obj, MainPageTabBase) and getattr(obj, 'tab_title', None) == tab_title:
91+
return obj
92+
raise NotImplementedError
93+
94+
95+
class MainPageTabBase(Page):
96+
def __init__(self, browser):
97+
Page.__init__(self, browser)
98+
99+
@property
100+
def element(self):
101+
return self.browser.find_element_by_id('content')
102+
103+
104+
class AddBookPage(MainPageTabBase):
105+
tab_title = _('Add')
106+
107+
def put_title(self, title):
108+
self.element.find_element_by_id('id_title').send_keys(title)
109+
110+
def put_text(self, text):
111+
self.element.find_element_by_id('id_text').send_keys(text)
112+
113+
def submit(self):
114+
self.browser.find_element_by_css_selector('table.editable button').click()
115+
return self.browser
116+
117+
118+
class BooksListPage(MainPageTabBase):
119+
tab_title = _('All')
120+
121+
@property
122+
def visible_books_count(self):
123+
return len(self.element.find_element_by_id('file-list').find_elements_by_tag_name('tr')) - 2
124+
125+

tests/integration/smoke_test.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from tests.integration.base import SeleniumTestCase, MainPage, _
2+
3+
class SmokeTest(SeleniumTestCase):
4+
5+
def test_add_book(self):
6+
user = self.create_super_user(do_login = True)
7+
8+
page = self.get_main_page()
9+
page.select_tab(_('All'))
10+
assert page.tab.visible_books_count == 0
11+
12+
page.select_tab(_('Add'))
13+
page.tab.put_title('Test title')
14+
page.tab.put_text('Test text')
15+
page.tab.submit()
16+
page.select_tab(_('All'))
17+
assert page.tab.visible_books_count == 1
18+

0 commit comments

Comments
 (0)