1 # -*- coding: utf-8 -*-
\r
5 from django.test import LiveServerTestCase
\r
6 from django.test.client import Client
\r
7 from django.conf import settings
\r
8 from django.contrib.auth.models import User
\r
9 from django.utils.translation import ugettext as _
\r
11 from selenium import webdriver
\r
14 class SeleniumTestCase(LiveServerTestCase):
\r
17 def setUpClass(cls):
\r
18 LiveServerTestCase.setUpClass()
\r
19 cls.browser = getattr(webdriver, os.environ.get('TEST_BROWSER', 'Firefox'))()
\r
20 cls.browser.implicitly_wait(5)
\r
23 def tearDownClass(cls):
\r
24 LiveServerTestCase.tearDownClass()
\r
28 self.browser.delete_all_cookies()
\r
30 def create_user(self, username='testuser', passwd='passwd', do_login=False):
\r
31 user = User.objects.create_user(username, '', passwd)
\r
32 user._plain_passwd = passwd
\r
34 self.login_user(user)
\r
37 def create_super_user(self, *args, **kwargs):
\r
38 user = self.create_user(*args, **kwargs)
\r
39 user.is_superuser = True
\r
43 def login_user(self, user):
\r
45 client.login(username=user.username, password=user._plain_passwd)
\r
47 if not self.browser.current_url.startswith(self.live_server_url):
\r
48 self.browser.get(self.live_server_url+'/not_existing_url')
\r
50 self.browser.find_element_by_tag_name('body') # Make sure the page is actually loaded before setting the cookie
\r
51 self.browser.delete_cookie(settings.SESSION_COOKIE_NAME)
\r
52 self.browser.add_cookie(dict(name=settings.SESSION_COOKIE_NAME,
\r
53 value=client.cookies[settings.SESSION_COOKIE_NAME].value,
\r
56 def get_main_page(self):
\r
57 self.browser.get(self.live_server_url)
\r
58 self.browser.find_element_by_tag_name('body')
\r
59 return MainPage(self.browser)
\r
63 def __init__(self, browser):
\r
64 self.browser = browser
\r
67 class MainPage(Page):
\r
69 def __init__(self, browser):
\r
70 Page.__init__(self, browser)
\r
75 return self.browser.find_element_by_tag_name('body')
\r
77 def select_tab(self, tab_title):
\r
78 for a in self.element.find_element_by_id('tabs-nav-left').find_elements_by_tag_name('a'):
\r
79 if a.text == tab_title:
\r
81 self.tab = find_tab_class(tab_title)(self.browser)
\r
83 raise Exception('Tab not found')
\r
86 def find_tab_class(tab_title):
\r
87 for obj in globals().values():
\r
88 if inspect.isclass(obj) and issubclass(obj, MainPageTabBase) and getattr(obj, 'tab_title', None) == tab_title:
\r
90 raise NotImplementedError
\r
93 class MainPageTabBase(Page):
\r
94 def __init__(self, browser):
\r
95 Page.__init__(self, browser)
\r
99 return self.browser.find_element_by_id('content')
\r
102 class AddBookPage(MainPageTabBase):
\r
103 tab_title = _('Add')
\r
105 def put_title(self, title):
\r
106 self.element.find_element_by_id('id_title').send_keys(title)
\r
108 def put_text(self, text):
\r
109 self.element.find_element_by_id('id_text').send_keys(text)
\r
112 self.browser.find_element_by_css_selector('table.editable button').click()
\r
113 return self.browser
\r
116 class BooksListPage(MainPageTabBase):
\r
117 tab_title = _('All')
\r
120 def visible_books_count(self):
\r
121 return len(self.element.find_element_by_id('file-list').find_elements_by_tag_name('tr')) - 2
\r