cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
__init__.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 # -*- coding: utf-8 -*-
21 ##
22 # @package src.wi.tests
23 #
24 # @author Piotr Wójcik
25 # @author Krzysztof Danielowski
26 # @date 11.10.2012
27 #
28 
29 from selenium import webdriver
30 from selenium.common.exceptions import NoSuchElementException
31 from selenium.webdriver.common.by import By
32 from selenium.webdriver.common.keys import Keys
33 from selenium.webdriver.support.wait import WebDriverWait
34 import re
35 import time
36 
37 TEST_SERVER = 'localhost:8000'
38 
39 
40 class WiTestCase:
41  def setUp(self):
42  self.driver = webdriver.Firefox()
43  self.driver.implicitly_wait(20)
44  self.TEST_USER = {'login': 'test_user', 'password': 'test1'}
45  self.TEST_admin_cm = {'login': 'test_cmadmin', 'password': 'test1', 'cm_password': 'test1'}
46  self.TEST_SERVER = TEST_SERVER
47  self.iso = "http://archive.ubuntu.com/ubuntu/dists/raring/main/installer-i386/current/images/netboot/mini.iso"
48  # self.iso = "http://nz11-agh1.ifj.edu.pl/public_users1/witekma/vmi/tiny-30-ctx.raw"
49 
50  def tearDown(self):
51  self.driver.quit()
52 
53  ##
54  #
55  # @parameter{xpath,string} xpath in which text should appears
56  # @parameter{text_list,list} list containing possible texts
57  # @parameter{max_time,int} describes how long method should search for text
58  # @parameter{sleep_time,double} describes gaps between consecutive searches
59  # @parameter{fail,boolean} describes if failure should brake test case
60  #
61  # Checks if one of texts has appeared in described xpath.
62  #
63  def wait_for_text(self, xpath, text_list, max_time=20, sleep_time=0.5, fail=True):
64  end_time = time.time() + max_time
65  while(True):
66  try:
67  el = self.driver.find_elements_by_xpath(xpath)
68  for text in text_list:
69  if text in "".join(i.text for i in el):
70  return True
71  except NoSuchElementException:
72  return False
73 
74  time.sleep(sleep_time)
75  if(time.time() > end_time):
76  break
77  if fail == False:
78  return False
79  self.fail("time out while searching for \"" + text + "\"")
80 
81  ##
82  #
83  # @parameter{text_list,list} is list containing possible texts
84  # @parameter{max_time,int} describes how long method should search for text
85  # @parameter{sleep_time,double} describes gaps between consecutive searches
86  #
87  # Checks if message with described text appears on the page
88  #
89  def wait_for_message(self, text_list, max_time=20, sleep_time=0.5):
90  end_time = time.time() + max_time
91  while(True):
92  try:
93  el = self.driver.find_elements_by_xpath("//div[@id='top-messages']/div")
94  for i in el:
95  if i.get_attribute("class") == "error":
96  i.find_element(By.CLASS_NAME, "remove-button").click()
97  self.fail(i.text)
98 
99  for text in text_list:
100  if text in "".join(i.text for i in el):
101  return True
102  except NoSuchElementException:
103  return False
104 
105  time.sleep(sleep_time)
106  if(time.time() > end_time):
107  break
108  self.fail("time out while seraching for \"" + text + "\"")
109 
110  ##
111  #
112  # @parameter{col_name,string} name of column in which text should appear
113  # @parameter{text,string} text
114  # @parameter{path_body_trs,string} xpath for searching rows of table
115  # @parameter{path_head_tds,string} xpath for searching head's cells of table
116  # @parameter{max_time,int} describes how long method should search for text
117  # @parameter{sleep_time,double} describes gaps between consecutive searches
118  # @parameter{check_data,dict}
119  # \n fields:
120  # @dictkey{path,string} path of table
121  # @dictkey{dict,dict} contains pairs: *col name* and *text* which are additional requirements for searched row
122  #
123  # Searchs for row in table and checks if it meets additional requirements described in check_data
124  # if not wait for table refresh and checks again
125  #
126  def __wait_for_table_update(self, col_name, text, check_data,
127  path_body_trs, path_head_tds, max_time=100, sleep_time=5):
128 
129  end_time = time.time() + max_time
130  while(True):
131  ind_col = self.__get_index_of_column(col_name, path_head_tds)
132  row = self.__find_row(ind_col, text, path_body_trs)
133  self.assertNotEqual(row, None, "Didn't find text %s in column %s" % (text, col_name))
134 
135  checked = True
136  for key, value in check_data['dict'].iteritems():
137  ind_key = self.__get_index_of_column(key, path_head_tds)
138  if not value in row[ind_key].text:
139  checked = False
140  if checked:
141  return True
142 
143  time.sleep(sleep_time)
144  if(time.time() > end_time):
145  break
146 
147  self.driver.refresh()
148  self.wait_for_text(check_data['path'], [text])
149 
150  el = self.driver.find_elements_by_xpath("//div[@id='top-messages']/div")
151  for i in el:
152  if i.get_attribute("class") == "error":
153  i.find_element(By.CLASS_NAME, "remove-button").click()
154  self.fail(i.text)
155 
156  self.fail("time out while seraching for \"" + text + "\"")
157 
158  ##
159  #
160  # @parameter{col_name,string} name of column
161  # @parameter{path_head_tds,string} xpath for searching head's cells of table
162  #
163  # Return index of column
164  #
165  def __get_index_of_column(self, col_name, path_head_tds):
166  ind_col = -1
167 
168  tds = self.driver.find_elements_by_xpath(path_head_tds)
169  for i in range(len(tds)):
170  td = tds[i]
171  if td.text == col_name:
172  ind_col = i
173 
174  self.assertNotEqual(ind_col, -1, "Column not found: %s" % (col_name))
175 
176  return ind_col
177 
178  ##
179  #
180  # @parameter{ind_col,int} index of column
181  # @parameter{text,string} text
182  # @parameter{path_body_trs,string} xpath for searching rows of table
183  #
184  # Return tds of row in which text appears in described column
185  #
186  def __find_row(self, ind_col, text, path_body_trs):
187  trs = self.driver.find_elements_by_xpath(path_body_trs)
188 
189  for i in range(len(trs)):
190  tds = trs[i].find_elements(By.TAG_NAME, "td")
191  if len(tds) > ind_col:
192  if text in tds[ind_col].text:
193  return tds
194 
195  return None
196 
197  ##
198  #
199  # @parameter{menu_item_text,string}
200  #
201  # Clicks on item in context menu
202  #
203  def __click_menu_item(self, menu_item_text):
204 
205  self.wait_for_text("//ul[@id='context-menu-list']/li", [menu_item_text])
206  items = self.driver.find_elements_by_xpath("//ul[@id='context-menu-list']/li")
207  for i in items:
208  if i.text == menu_item_text:
209  i.click()
210  return
211 
212  ##
213  #
214  # @parameter{col_name,string} name of column which together with text describe row
215  # @parameter{text,string} text
216  # @parameter{action_name,string} name of column which together with row describes cell
217  # @parameter{element,string} element in cell which sholud be clicked
218  # @parameter{path_body_trs,string} xpath for searching rows of table
219  # @parameter{path_head_tds,string} xpath for searching head's cells of table
220  # @parameter{check_data,dict}
221  # \n fields:
222  # @dictkey{path,string} path of table
223  # @dictkey{dict,dict} contains pairs: *col name* and *text* which are additional requirements for searched row
224  #
225  # Clicks on cell described by parameters
226  #
227  def cell_click(self, col_name, text, check_data=None, action_name="Actions", element="div",
228  path_head_tds="//table[@id='item-list']/thead/tr/td",
229  path_body_trs="//table[@id='item-list']/tbody/tr"):
230 
231  if check_data != None:
232  self.__wait_for_table_update(col_name, text, check_data, path_body_trs, path_head_tds)
233 
234  ind_col = self.__get_index_of_column(col_name, path_head_tds)
235  row = self.__find_row(ind_col, text, path_body_trs)
236  self.assertNotEqual(row, None, "Didn't find text %s in column %s" % (text, col_name))
237 
238  ind_action = self.__get_index_of_column(action_name, path_head_tds)
239 
240  row[ind_action].find_element(By.TAG_NAME, element).click()
241 
242  ##
243  #
244  # @parameter{col_name,string} name of column which together with text describe row
245  # @parameter{text,string} text
246  # @parameter{path_body_trs,string} xpath for searching rows of table
247  # @parameter{path_head_tds,string} xpath for searching head's cells of table
248  # @parameter{check_data,dict}
249  # \n fields:
250  # @dictkey{path,string} path of table
251  # @dictkey{dict,dict} contains pairs: *col name* and *text* which are additional requirements for searched row
252  #
253  # Clicks on row described by parameters
254  #
255  def row_click(self, col_name, text, check_data=None,
256  path_head_tds="//table[@id='item-list']/thead/tr/td",
257  path_body_trs="//table[@id='item-list']/tbody/tr"):
258  if check_data:
259  self.__wait_for_table_update(col_name, text, check_data, path_body_trs, path_head_tds)
260 
261  ind_col = self.__get_index_of_column(col_name, path_head_tds)
262  row = self.__find_row(ind_col, text, path_body_trs)
263  self.assertNotEqual(row, None, "Didn't find text %s in column %s" % (text, col_name))
264 
265  row[ind_col].click()
266 
267  ##
268  #
269  # @parameter{col_name,string} name of column which together with text describe row
270  # @parameter{text,string} text
271  # @parameter{action_name,string} name of column which together with row describe cell
272  # @parameter{menu_item_text,string} xpath for searching rows of table
273  # @parameter{check_data,dict}
274  # \n fields:
275  # @dictkey{path,string} path of table
276  # @dictkey{dict,dict} contains pairs: *col name* and *text* which are additional requirements for searched row
277  #
278  # Clicks on cell described by parameters and then clicks on item in context menu
279  #
280  def menu_click(self, col_name, text, menu_item_text, check_data=None, action_name="Actions"):
281 
282  self.cell_click(col_name, text, check_data, action_name)
283  self.__click_menu_item(menu_item_text)
284 
285  def login_testuser(self, user):
286  driver = self.driver
287  self.base_url = self.TEST_SERVER
288 
289  driver.get(self.base_url + '/auth/login/')
290 
291  driver.find_element_by_id("id_username").clear()
292  driver.find_element_by_id("id_username").send_keys(user['login'])
293  driver.find_element_by_id("id_password").clear()
294  driver.find_element_by_id("id_password").send_keys(user['password'])
295  driver.find_element_by_css_selector("input.big_button").click()
296 
297  self.wait_for_text("//div[@id='header']/ul/li", ["test"])
298  self.change_language()
299 
300  def login_cm_testuser(self):
301  driver = self.driver
302  self.base_url = self.TEST_SERVER
303 
304  driver.get(self.base_url + '/admin_cm/login/')
305 
306  driver.find_element_by_id("id_password").clear()
307  driver.find_element_by_id("id_password").send_keys(self.TEST_admin_cm['cm_password'])
308  driver.find_element_by_css_selector("input.big_button").click()
309 
310  self.wait_for_text("//div[@id='header']/ul/li", ["test"])
311  self.change_language()
312 
313  def change_language(self):
314  driver = self.driver
315  driver.find_element_by_xpath("//form[@id='languageForm']/a/span[2]").click()
316  driver.find_element_by_xpath("//a[contains(text(),'English')]").click()
317