attachment list for links + bad links in red
[redakcja.git] / tests / integration / base.py
1 # -*- coding: utf-8 -*-\r
2 import os\r
3 import inspect\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\r
9 from django.utils.translation import ugettext as _\r
10 \r
11 from selenium import webdriver\r
12 \r
13 \r
14 class SeleniumTestCase(LiveServerTestCase):\r
15 \r
16     @classmethod\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
21 \r
22     @classmethod\r
23     def tearDownClass(cls):\r
24         LiveServerTestCase.tearDownClass()\r
25         cls.browser.quit()\r
26 \r
27     def setUp(self):\r
28         self.browser.delete_all_cookies()\r
29 \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
33         if do_login:\r
34             self.login_user(user)\r
35         return user\r
36 \r
37     def create_super_user(self, *args, **kwargs):\r
38         user = self.create_user(*args, **kwargs)\r
39         user.is_superuser = True\r
40         user.save()\r
41         return user\r
42 \r
43     def login_user(self, user):\r
44         client = Client()\r
45         client.login(username=user.username, password=user._plain_passwd)\r
46 \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
49 \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
54                                      path='/'))\r
55 \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
60 \r
61 \r
62 class Page(object):\r
63     def __init__(self, browser):\r
64         self.browser = browser\r
65 \r
66 \r
67 class MainPage(Page):\r
68 \r
69     def __init__(self, browser):\r
70         Page.__init__(self, browser)\r
71         self.tab = None\r
72 \r
73     @property\r
74     def element(self):\r
75         return self.browser.find_element_by_tag_name('body')\r
76 \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
80                 a.click()\r
81                 self.tab = find_tab_class(tab_title)(self.browser)\r
82                 return\r
83         raise Exception('Tab not found')\r
84 \r
85 \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
89             return obj\r
90     raise NotImplementedError\r
91 \r
92 \r
93 class MainPageTabBase(Page):\r
94     def __init__(self, browser):\r
95         Page.__init__(self, browser)\r
96 \r
97     @property\r
98     def element(self):\r
99         return self.browser.find_element_by_id('content')\r
100 \r
101 \r
102 class AddBookPage(MainPageTabBase):\r
103     tab_title = _('Add')\r
104 \r
105     def put_title(self, title):\r
106         self.element.find_element_by_id('id_title').send_keys(title)\r
107 \r
108     def put_text(self, text):\r
109         self.element.find_element_by_id('id_text').send_keys(text)\r
110 \r
111     def submit(self):\r
112         self.browser.find_element_by_css_selector('table.editable button').click()\r
113         return self.browser\r
114 \r
115 \r
116 class BooksListPage(MainPageTabBase):\r
117     tab_title = _('All')\r
118 \r
119     @property\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