3 from urlparse import urlparse
\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, Permission
\r
9 from django.utils.translation import ugettext as _
\r
11 from selenium import webdriver, selenium
\r
12 from selenium.webdriver.support.wait import WebDriverWait
\r
15 class SeleniumTestCase(LiveServerTestCase):
\r
18 def setUpClass(cls):
\r
19 LiveServerTestCase.setUpClass()
\r
20 cls.browser = getattr(webdriver, os.environ.get('TEST_BROWSER', 'Firefox'))()
\r
21 cls.browser.implicitly_wait(5)
\r
24 def tearDownClass(cls):
\r
25 LiveServerTestCase.tearDownClass()
\r
29 self.browser.delete_all_cookies()
\r
31 def create_user(self, username = 'testuser', passwd = 'passwd', do_login = False):
\r
32 user = User.objects.create_user(username, '', passwd)
\r
33 user._plain_passwd = passwd
\r
35 self.login_user(user)
\r
38 def create_super_user(self, *args, **kwargs):
\r
39 user = self.create_user(*args, **kwargs)
\r
40 user.is_superuser = True
\r
44 def login_user(self, user):
\r
46 client.login(username = user.username, password = user._plain_passwd)
\r
48 if not self.browser.current_url.startswith(self.live_server_url):
\r
49 self.browser.get(self.live_server_url+'/not_existing_url')
\r
51 self.browser.find_element_by_tag_name('body') # Make sure the page is actually loaded before setting the cookie
\r
52 self.browser.delete_cookie(settings.SESSION_COOKIE_NAME)
\r
53 self.browser.add_cookie(dict(name = settings.SESSION_COOKIE_NAME,
\r
54 value = client.cookies[settings.SESSION_COOKIE_NAME].value,
\r
58 def get_main_page(self):
\r
59 self.browser.get(self.live_server_url)
\r
60 self.browser.find_element_by_tag_name('body')
\r
61 return MainPage(self.browser)
\r
65 def __init__(self, browser):
\r
66 self.browser = browser
\r
69 class MainPage(Page):
\r
71 def __init__(self, browser):
\r
72 Page.__init__(self, browser)
\r
77 return self.browser.find_element_by_tag_name('body')
\r
79 def select_tab(self, tab_title):
\r
80 for a in self.element.find_element_by_id('tabs-nav-left').find_elements_by_tag_name('a'):
\r
81 if a.text == tab_title:
\r
83 self.tab = find_tab_class(tab_title)(self.browser)
\r
85 raise Exception, 'Tab not found'
\r
88 def find_tab_class(tab_title):
\r
89 for obj in globals().values():
\r
90 if inspect.isclass(obj) and issubclass(obj, MainPageTabBase) and getattr(obj, 'tab_title', None) == tab_title:
\r
92 raise NotImplementedError
\r
95 class MainPageTabBase(Page):
\r
96 def __init__(self, browser):
\r
97 Page.__init__(self, browser)
\r
101 return self.browser.find_element_by_id('content')
\r
104 class AddBookPage(MainPageTabBase):
\r
105 tab_title = _('Add')
\r
107 def put_title(self, title):
\r
108 self.element.find_element_by_id('id_title').send_keys(title)
\r
110 def put_text(self, text):
\r
111 self.element.find_element_by_id('id_text').send_keys(text)
\r
114 self.browser.find_element_by_css_selector('table.editable button').click()
\r
115 return self.browser
\r
118 class BooksListPage(MainPageTabBase):
\r
119 tab_title = _('All')
\r
122 def visible_books_count(self):
\r
123 return len(self.element.find_element_by_id('file-list').find_elements_by_tag_name('tr')) - 2
\r