1 # -*- coding: utf-8 -*-
\r
3 # This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later.
\r
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
\r
9 from django.test import LiveServerTestCase
\r
10 from django.test.client import Client
\r
11 from django.conf import settings
\r
12 from django.contrib.auth.models import User
\r
13 from django.utils.translation import ugettext as _
\r
15 from selenium import webdriver
\r
18 class SeleniumTestCase(LiveServerTestCase):
\r
21 def setUpClass(cls):
\r
22 LiveServerTestCase.setUpClass()
\r
23 cls.browser = getattr(webdriver, os.environ.get('TEST_BROWSER', 'Firefox'))()
\r
24 cls.browser.implicitly_wait(5)
\r
27 def tearDownClass(cls):
\r
28 LiveServerTestCase.tearDownClass()
\r
32 self.browser.delete_all_cookies()
\r
34 def create_user(self, username='testuser', passwd='passwd', do_login=False):
\r
35 user = User.objects.create_user(username, '', passwd)
\r
36 user._plain_passwd = passwd
\r
38 self.login_user(user)
\r
41 def create_super_user(self, *args, **kwargs):
\r
42 user = self.create_user(*args, **kwargs)
\r
43 user.is_superuser = True
\r
47 def login_user(self, user):
\r
49 client.login(username=user.username, password=user._plain_passwd)
\r
51 if not self.browser.current_url.startswith(self.live_server_url):
\r
52 self.browser.get(self.live_server_url+'/not_existing_url')
\r
54 self.browser.find_element_by_tag_name('body') # Make sure the page is actually loaded before setting the cookie
\r
55 self.browser.delete_cookie(settings.SESSION_COOKIE_NAME)
\r
56 self.browser.add_cookie(dict(name=settings.SESSION_COOKIE_NAME,
\r
57 value=client.cookies[settings.SESSION_COOKIE_NAME].value,
\r
60 def get_main_page(self):
\r
61 self.browser.get(self.live_server_url)
\r
62 self.browser.find_element_by_tag_name('body')
\r
63 return MainPage(self.browser)
\r
67 def __init__(self, browser):
\r
68 self.browser = browser
\r
71 class MainPage(Page):
\r
73 def __init__(self, browser):
\r
74 Page.__init__(self, browser)
\r
79 return self.browser.find_element_by_tag_name('body')
\r
81 def select_tab(self, tab_title):
\r
82 for a in self.element.find_element_by_id('tabs-nav-left').find_elements_by_tag_name('a'):
\r
83 if a.text == tab_title:
\r
85 self.tab = find_tab_class(tab_title)(self.browser)
\r
87 raise Exception('Tab not found')
\r
90 def find_tab_class(tab_title):
\r
91 for obj in globals().values():
\r
92 if inspect.isclass(obj) and issubclass(obj, MainPageTabBase) and getattr(obj, 'tab_title', None) == tab_title:
\r
94 raise NotImplementedError
\r
97 class MainPageTabBase(Page):
\r
98 def __init__(self, browser):
\r
99 Page.__init__(self, browser)
\r
103 return self.browser.find_element_by_id('content')
\r
106 class AddBookPage(MainPageTabBase):
\r
107 tab_title = _('Add')
\r
109 def put_title(self, title):
\r
110 self.element.find_element_by_id('id_title').send_keys(title)
\r
112 def put_text(self, text):
\r
113 self.element.find_element_by_id('id_text').send_keys(text)
\r
116 self.browser.find_element_by_css_selector('table.editable button').click()
\r
117 return self.browser
\r
120 class BooksListPage(MainPageTabBase):
\r
121 tab_title = _('All')
\r
124 def visible_books_count(self):
\r
125 return len(self.element.find_element_by_id('file-list').find_elements_by_tag_name('tr')) - 2
\r