Fix annotations window.
[redakcja.git] / tests / integration / base.py
1 import os\r
2 import inspect\r
3 from urlparse import urlparse\r
4 \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
10 \r
11 from selenium import webdriver, selenium\r
12 from selenium.webdriver.support.wait import WebDriverWait\r
13 \r
14 \r
15 class SeleniumTestCase(LiveServerTestCase):\r
16 \r
17     @classmethod\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
22         \r
23     @classmethod\r
24     def tearDownClass(cls):\r
25         LiveServerTestCase.tearDownClass()\r
26         cls.browser.quit()\r
27         \r
28     def setUp(self):\r
29         self.browser.delete_all_cookies()\r
30     \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
34         if do_login:\r
35             self.login_user(user)\r
36         return user\r
37     \r
38     def create_super_user(self, *args, **kwargs):\r
39         user = self.create_user(*args, **kwargs)\r
40         user.is_superuser = True\r
41         user.save()\r
42         return user\r
43         \r
44     def login_user(self, user):\r
45         client = Client()\r
46         client.login(username = user.username, password = user._plain_passwd)\r
47 \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
50             \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
55                                      path = '/')\r
56                                )\r
57         \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
62 \r
63         \r
64 class Page(object):\r
65     def __init__(self, browser):\r
66         self.browser = browser\r
67     \r
68     \r
69 class MainPage(Page):\r
70 \r
71     def __init__(self, browser):\r
72         Page.__init__(self, browser)\r
73         self.tab = None\r
74     \r
75     @property\r
76     def element(self):\r
77         return self.browser.find_element_by_tag_name('body')\r
78     \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
82                 a.click()\r
83                 self.tab = find_tab_class(tab_title)(self.browser)\r
84                 return\r
85         raise Exception, 'Tab not found'\r
86         \r
87                 \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
91             return obj\r
92     raise NotImplementedError\r
93                 \r
94 \r
95 class MainPageTabBase(Page):\r
96     def __init__(self, browser):\r
97         Page.__init__(self, browser)\r
98     \r
99     @property\r
100     def element(self):\r
101         return self.browser.find_element_by_id('content')\r
102         \r
103 \r
104 class AddBookPage(MainPageTabBase):\r
105     tab_title = _('Add')\r
106     \r
107     def put_title(self, title):\r
108         self.element.find_element_by_id('id_title').send_keys(title)\r
109         \r
110     def put_text(self, text):\r
111         self.element.find_element_by_id('id_text').send_keys(text)\r
112         \r
113     def submit(self):\r
114         self.browser.find_element_by_css_selector('table.editable button').click()\r
115         return self.browser\r
116         \r
117     \r
118 class BooksListPage(MainPageTabBase):\r
119     tab_title = _('All')\r
120     \r
121     @property\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
124         \r
125