From: Aleksander Ɓukasz Date: Tue, 2 Apr 2013 07:36:13 +0000 (+0200) Subject: Introducing integration tests X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/64def8585cd770750020addd02e959b2f9a9426f?ds=inline Introducing integration tests $ TEST_BROWSER=Chrome python redakcja/manage.py test --settings settings.selenium tests/integration/ --- diff --git a/.gitignore b/.gitignore index 866239ad..318e02d0 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,7 @@ nbproject/* .pydevproject .settings -node_modules \ No newline at end of file +node_modules + +/static_test +chromedriver.log \ No newline at end of file diff --git a/redakcja/settings/integration_test.py b/redakcja/settings/integration_test.py new file mode 100644 index 00000000..ba477bb0 --- /dev/null +++ b/redakcja/settings/integration_test.py @@ -0,0 +1,7 @@ +from redakcja.settings.test import * + +NOSE_ARGS = () + +STATIC_ROOT_SYMLINK = os.path.dirname(STATIC_ROOT) + '_test' +STATICFILES_DIRS.append(STATIC_ROOT_SYMLINK) + diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py new file mode 100644 index 00000000..5c572ea3 --- /dev/null +++ b/tests/integration/__init__.py @@ -0,0 +1,5 @@ +import os +from django.conf import settings + +if not os.path.exists(settings.STATIC_ROOT_SYMLINK): + os.symlink(settings.STATIC_ROOT, settings.STATIC_ROOT_SYMLINK) \ No newline at end of file diff --git a/tests/integration/base.py b/tests/integration/base.py new file mode 100644 index 00000000..c2774c97 --- /dev/null +++ b/tests/integration/base.py @@ -0,0 +1,125 @@ +import os +import inspect +from urlparse import urlparse + +from django.test import LiveServerTestCase +from django.test.client import Client +from django.conf import settings +from django.contrib.auth.models import User, Permission +from django.utils.translation import ugettext as _ + +from selenium import webdriver, selenium +from selenium.webdriver.support.wait import WebDriverWait + + +class SeleniumTestCase(LiveServerTestCase): + + @classmethod + def setUpClass(cls): + LiveServerTestCase.setUpClass() + cls.browser = getattr(webdriver, os.environ.get('TEST_BROWSER', 'Firefox'))() + cls.browser.implicitly_wait(5) + + @classmethod + def tearDownClass(cls): + LiveServerTestCase.tearDownClass() + cls.browser.quit() + + def setUp(self): + self.browser.delete_all_cookies() + + def create_user(self, username = 'testuser', passwd = 'passwd', do_login = False): + user = User.objects.create_user(username, '', passwd) + user._plain_passwd = passwd + if do_login: + self.login_user(user) + return user + + def create_super_user(self, *args, **kwargs): + user = self.create_user(*args, **kwargs) + user.is_superuser = True + user.save() + return user + + def login_user(self, user): + client = Client() + client.login(username = user.username, password = user._plain_passwd) + + if not self.browser.current_url.startswith(self.live_server_url): + self.browser.get(self.live_server_url+'/not_existing_url') + + self.browser.find_element_by_tag_name('body') # Make sure the page is actually loaded before setting the cookie + self.browser.delete_cookie(settings.SESSION_COOKIE_NAME) + self.browser.add_cookie(dict(name = settings.SESSION_COOKIE_NAME, + value = client.cookies[settings.SESSION_COOKIE_NAME].value, + path = '/') + ) + + def get_main_page(self): + self.browser.get(self.live_server_url) + self.browser.find_element_by_tag_name('body') + return MainPage(self.browser) + + +class Page(object): + def __init__(self, browser): + self.browser = browser + + +class MainPage(Page): + + def __init__(self, browser): + Page.__init__(self, browser) + self.tab = None + + @property + def element(self): + return self.browser.find_element_by_tag_name('body') + + def select_tab(self, tab_title): + for a in self.element.find_element_by_id('tabs-nav-left').find_elements_by_tag_name('a'): + if a.text == tab_title: + a.click() + self.tab = find_tab_class(tab_title)(self.browser) + return + raise Exception, 'Tab not found' + + +def find_tab_class(tab_title): + for obj in globals().values(): + if inspect.isclass(obj) and issubclass(obj, MainPageTabBase) and getattr(obj, 'tab_title', None) == tab_title: + return obj + raise NotImplementedError + + +class MainPageTabBase(Page): + def __init__(self, browser): + Page.__init__(self, browser) + + @property + def element(self): + return self.browser.find_element_by_id('content') + + +class AddBookPage(MainPageTabBase): + tab_title = _('Add') + + def put_title(self, title): + self.element.find_element_by_id('id_title').send_keys(title) + + def put_text(self, text): + self.element.find_element_by_id('id_text').send_keys(text) + + def submit(self): + self.browser.find_element_by_css_selector('table.editable button').click() + return self.browser + + +class BooksListPage(MainPageTabBase): + tab_title = _('All') + + @property + def visible_books_count(self): + return len(self.element.find_element_by_id('file-list').find_elements_by_tag_name('tr')) - 2 + + \ No newline at end of file diff --git a/tests/integration/smoke_test.py b/tests/integration/smoke_test.py new file mode 100644 index 00000000..fb29a73c --- /dev/null +++ b/tests/integration/smoke_test.py @@ -0,0 +1,18 @@ +from tests.integration.base import SeleniumTestCase, MainPage, _ + +class SmokeTest(SeleniumTestCase): + + def test_add_book(self): + user = self.create_super_user(do_login = True) + + page = self.get_main_page() + page.select_tab(_('All')) + assert page.tab.visible_books_count == 0 + + page.select_tab(_('Add')) + page.tab.put_title('Test title') + page.tab.put_text('Test text') + page.tab.submit() + page.select_tab(_('All')) + assert page.tab.visible_books_count == 1 + \ No newline at end of file