基础类封装-Selenium等待类库封装

  1 #!/usr/bin/env python3
  2 # -*- coding: utf-8 -*-
  3 # @Time    : 2019/10/14 23:37
  4 # @Author  : Tang Yiwei
  5 # @Email   : 892398433@qq.com
  6 # @File    : WaitUtil.py
  7 # @Software: PyCharm
  8 
  9 from selenium.webdriver.support import expected_conditions as EC
 10 from util.ObjectMap import *
 11 from selenium.common.exceptions import NoSuchElementException,TimeoutException
 12 
 13 class WaitUtil():
 14 
 15     def __init__(self,driver):
 16         self.driver = driver
 17         self.wait = WebDriverWait(self.driver,30)
 18 
 19     def presence_of_element_located(self,selector):
 20         """显示的等待页面元素出现在DOM中,但不一定可见,存在则返回该元素的页面对象,定位器传入元祖类型"""
 21         try:
 22             if '=>' not in selector:
 23                 element = self.wait.until(EC.presence_of_element_located(findElement(self.driver, selector)))
 24                 return element
 25             else:
 26                 locationType = selector.split('=>')[0]
 27                 locatorExpression = selector.split('=>')[1]
 28                 if locationType.lower() in ['i', 'id']:
 29                     element = self.wait.until(EC.presence_of_element_located((By.ID, locatorExpression)))
 30                 elif locationType.lower() in ['n', 'name']:
 31                     element = self.wait.until(EC.presence_of_element_located((By.NAME, locatorExpression)))
 32                 elif locationType.lower() in ['c', 'class']:
 33                     element = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, locatorExpression)))
 34                 elif locationType.lower() in ['l', 'link']:
 35                     element = self.wait.until(EC.presence_of_element_located((By.LINK_TEXT, locatorExpression)))
 36                 elif locationType.lower() in ['p', 'partial_link']:
 37                     element = self.wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, locatorExpression)))
 38                 elif locationType.lower() in ['t', 'tag_name']:
 39                     element = self.wait.until(EC.presence_of_element_located((By.TAG_NAME, locatorExpression)))
 40                 elif locationType.lower() in ['x', 'xpath']:
 41                     element = self.wait.until(EC.presence_of_element_located((By.XPATH, locatorExpression)))
 42                 elif locationType.lower() in ['s', 'css']:
 43                     element = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, locatorExpression)))
 44                 return element
 45         except TimeoutException as e:
 46             print(traceback.print_exc())
 47         except NoSuchElementException as e:
 48             print(traceback.print_exc())
 49         except Exception as e:
 50             raise e
 51 
 52     def frame_to_be_available_and_switch_to_it(self,selector):
 53         """检查Frame是否存在,存在则切换进frame控件中,定位器传入元祖类型"""
 54         try:
 55             if '=>' not in selector:
 56                 self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,selector)))
 57             else:
 58                 locationType = selector.split('=>')[0]
 59                 locatorExpression = selector.split('=>')[1]
 60                 if locationType.lower() in ['i', 'id']:
 61                     self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, locatorExpression)))
 62                 elif locationType.lower() in ['n', 'name']:
 63                     self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME, locatorExpression)))
 64                 elif locationType.lower() in ['c', 'class']:
 65                     self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, locatorExpression)))
 66                 elif locationType.lower() in ['l', 'link']:
 67                     self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.LINK_TEXT, locatorExpression)))
 68                 elif locationType.lower() in ['p', 'partial_link']:
 69                     self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.PARTIAL_LINK_TEXT, locatorExpression)))
 70                 elif locationType.lower() in ['t', 'tag_name']:
 71                     self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, locatorExpression)))
 72                 elif locationType.lower() in ['x', 'xpath']:
 73                     self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, locatorExpression)))
 74                 elif locationType.lower() in ['s', 'css']:
 75                     self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, locatorExpression)))
 76         except TimeoutException as e:
 77             print(traceback.print_exc())
 78         except NoSuchElementException as e:
 79                 print(traceback.print_exc())
 80         except Exception as e:
 81             raise e
 82 
 83     def visibility_of_element_located(self,selector):
 84         """显示等待页面元素的出现在DOM中,并且可见,存在则返回该页面元素对象,定位器传入元祖类型"""
 85         try:
 86             if '=>' not in selector:
 87                 element = self.wait.until(EC.visibility_of_element_located((By.ID,selector)))
 88                 return element
 89             else:
 90                 locationType = selector.split('=>')[0]
 91                 locatorExpression = selector.split('=>')[1]
 92                 if locationType.lower() in ['i', 'id']:
 93                     element = self.wait.until(EC.visibility_of_element_located((By.ID, locatorExpression)))
 94                 elif locationType.lower() in ['n', 'name']:
 95                     element = self.wait.until(EC.visibility_of_element_located((By.NAME, locatorExpression)))
 96                 elif locationType.lower() in ['c', 'class']:
 97                     element = self.wait.until(EC.visibility_of_element_located((By.CLASS_NAME, locatorExpression)))
 98                 elif locationType.lower() in ['l', 'link']:
 99                     element = self.wait.until(EC.visibility_of_element_located((By.LINK_TEXT, locatorExpression)))
100                 elif locationType.lower() in ['p', 'partial_link']:
101                     element = self.wait.until(EC.visibility_of_element_located((By.PARTIAL_LINK_TEXT, locatorExpression)))
102                 elif locationType.lower() in ['t', 'tag_name']:
103                     element = self.wait.until(EC.visibility_of_element_located((By.TAG_NAME, locatorExpression)))
104                 elif locationType.lower() in ['x', 'xpath']:
105                     element = self.wait.until(EC.visibility_of_element_located((By.XPATH, locatorExpression)))
106                 elif locationType.lower() in ['s', 'css']:
107                     element = self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, locatorExpression)))
108                 return element
109         except TimeoutException as e:
110             print(traceback.print_exc())
111         except NoSuchElementException as e:
112             print(traceback.print_exc())
113         except Exception as e:
114             raise e
115 
116     def alert_is_present(self):
117         """判断弹框是否弹出"""
118         try:
119             alertText = self.wait.until(EC.alert_is_present()).text
120             return alertText
121         except Exception as e:
122             raise e
123 
124     def element_located_selection_state_to_be(self, selector, state):
125         """
126         判断一个元素的状态是否是给定的选择状态,第一个传入的参数是一个定位器,定位器是一个元组(by,xpath);
127         第二个元素表示状态,True表示选中状态,False表示未选中状态;相等返回True,不相等返回False;
128         """
129         try:
130             if '=>' not in selector:
131                 element = self.wait.until(EC.element_located_selection_state_to_be((By.ID,selector),state))
132                 return element
133             else:
134                 locationType = selector.split('=>')[0]
135                 locatorExpression = selector.split('=>')[1]
136                 if locationType.lower() in ['i', 'id']:
137                     element = self.wait.until(EC.element_located_selection_state_to_be((By.ID, locatorExpression),state))
138                 elif locationType.lower() in ['n', 'name']:
139                     element = self.wait.until(EC.element_located_selection_state_to_be((By.NAME, locatorExpression),state))
140                 elif locationType.lower() in ['c', 'class']:
141                     element = self.wait.until(EC.element_located_selection_state_to_be((By.CLASS_NAME, locatorExpression),state))
142                 elif locationType.lower() in ['l', 'link']:
143                     element = self.wait.until(EC.element_located_selection_state_to_be((By.LINK_TEXT, locatorExpression),state))
144                 elif locationType.lower() in ['p', 'partial_link']:
145                     element = self.wait.until(EC.element_located_selection_state_to_be((By.PARTIAL_LINK_TEXT, locatorExpression),state))
146                 elif locationType.lower() in ['t', 'tag_name']:
147                     element = self.wait.until(EC.element_located_selection_state_to_be((By.TAG_NAME, locatorExpression),state))
148                 elif locationType.lower() in ['x', 'xpath']:
149                     element = self.wait.until(EC.element_located_selection_state_to_be((By.XPATH, locatorExpression),state))
150                 elif locationType.lower() in ['s', 'css']:
151                     element = self.wait.until(EC.element_located_selection_state_to_be((By.CSS_SELECTOR, locatorExpression),state))
152                 return element
153         except TimeoutException as e:
154             print(traceback.print_exc())
155         except NoSuchElementException as e:
156             print(traceback.print_exc())
157         except Exception as e:
158             raise e
159 
160     def element_selection_state_to_be(self,selector,state):
161         """
162         判断给定的元素是否被选中,第一个参数是WebDriver对象;
163         第二个参数是期望的元素状态,相等返回True,否则返回False;
164         """
165         try:
166             self.wait.until(EC.element_selection_state_to_be(findElement(self.driver,selector),state))
167         except TimeoutException as e:
168             print(traceback.print_exc())
169         except NoSuchElementException as e:
170             print(traceback.print_exc())
171         except Exception as e:
172             raise e
173 
174     def element_to_be_selected(self,selector):
175         """
176         期望元素处于选中状态,参数为一个WebDriver实例对象
177         """
178         try:
179             element = self.wait.until(EC.element_to_be_selected(findElement(self.driver,selector)))
180             return element
181         except TimeoutException as e:
182             print(traceback.print_exc())
183         except NoSuchElementException as e:
184             print(traceback.print_exc())
185         except Exception as e:
186             raise e
187 
188     def element_to_be_clickable(self,selector):
189         """
190         判断元素是否可见并且能被点击,条件满足,返回该元素的页面对象,否则返回False,传入元祖类型定位器
191         """
192         try:
193             if '=>' not in selector:
194                 element = self.wait.until(EC.element_to_be_clickable((By.ID, selector)))
195                 return element
196             else:
197                 locationType = selector.split('=>')[0]
198                 locatorExpression = selector.split('=>')[1]
199                 if locationType.lower() in ['i', 'id']:
200                     element = self.wait.until(EC.element_to_be_clickable((By.ID, locatorExpression)))
201                 elif locationType.lower() in ['n', 'name']:
202                     element = self.wait.until(EC.element_to_be_clickable((By.NAME, locatorExpression)))
203                 elif locationType.lower() in ['c', 'class']:
204                     element = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, locatorExpression)))
205                 elif locationType.lower() in ['l', 'link']:
206                     element = self.wait.until(EC.element_to_be_clickable((By.LINK_TEXT, locatorExpression)))
207                 elif locationType.lower() in ['p', 'partial_link']:
208                     element = self.wait.until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, locatorExpression)))
209                 elif locationType.lower() in ['t', 'tag_name']:
210                     element = self.wait.until(EC.element_to_be_clickable((By.TAG_NAME, locatorExpression)))
211                 elif locationType.lower() in ['x', 'xpath']:
212                     element = self.wait.until(EC.element_to_be_clickable((By.XPATH, locatorExpression)))
213                 elif locationType.lower() in ['s', 'css']:
214                     element = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, locatorExpression)))
215                 return element
216         except TimeoutException as e:
217             print(traceback.print_exc())
218         except NoSuchElementException as e:
219             print(traceback.print_exc())
220         except Exception as e:
221             raise e
222 
223     def invisibility_of_element_located(self,selector):
224         """
225         希望某个元素不可见或者不存在于DOM中,满足条件返回True,否则返回定位到的元素对象,定位器传入元祖类型
226         """
227         try:
228             if '=>' not in selector:
229                 element = self.wait.until(EC.invisibility_of_element_located((By.ID, selector)))
230                 return element
231             else:
232                 locationType = selector.split('=>')[0]
233                 locatorExpression = selector.split('=>')[1]
234                 if locationType.lower() in ['i', 'id']:
235                     element = self.wait.until(EC.invisibility_of_element_located((By.ID, locatorExpression)))
236                 elif locationType.lower() in ['n', 'name']:
237                     element = self.wait.until(EC.invisibility_of_element_located((By.NAME, locatorExpression)))
238                 elif locationType.lower() in ['c', 'class']:
239                     element = self.wait.until(EC.invisibility_of_element_located((By.CLASS_NAME, locatorExpression)))
240                 elif locationType.lower() in ['l', 'link']:
241                     element = self.wait.until(EC.invisibility_of_element_located((By.LINK_TEXT, locatorExpression)))
242                 elif locationType.lower() in ['p', 'partial_link']:
243                     element = self.wait.until(EC.invisibility_of_element_located((By.PARTIAL_LINK_TEXT, locatorExpression)))
244                 elif locationType.lower() in ['t', 'tag_name']:
245                     element = self.wait.until(EC.invisibility_of_element_located((By.TAG_NAME, locatorExpression)))
246                 elif locationType.lower() in ['x', 'xpath']:
247                     element = self.wait.until(EC.invisibility_of_element_located((By.XPATH, locatorExpression)))
248                 elif locationType.lower() in ['s', 'css']:
249                     element = self.wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, locatorExpression)))
250                 return element
251         except TimeoutException as e:
252             print(traceback.print_exc())
253         except NoSuchElementException as e:
254             print(traceback.print_exc())
255         except Exception as e:
256             raise e
257 
258     def visibility_of(self,selector):
259         """
260         希望某个元素出现在页面的DOM中,并且可见,如果满足条件返回该元素的页面元素对象,传入WebElement对象
261         """
262         try:
263             element = self.wait.until(EC.visibility_of((findElement(self.driver,selector))))
264             return element
265         except TimeoutException as e:
266             print(traceback.print_exc())
267         except NoSuchElementException as e:
268             print(traceback.print_exc())
269         except Exception as e:
270             raise e
271 
272     def visibility_of_any_elements_located(self,selector):
273         """
274         判断页面上至少有一个元素可见,返回满足条件的所有页面元素对象,传入元祖类型的定位器
275         """
276         try:
277             if '=>' not in selector:
278                 element = self.wait.until(EC.visibility_of_any_elements_located((By.ID, selector)))
279                 return element
280             else:
281                 locationType = selector.split('=>')[0]
282                 locatorExpression = selector.split('=>')[1]
283                 if locationType.lower() in ['i', 'id']:
284                     element = self.wait.until(EC.visibility_of_any_elements_located((By.ID, locatorExpression)))
285                 elif locationType.lower() in ['n', 'name']:
286                     element = self.wait.until(EC.visibility_of_any_elements_located((By.NAME, locatorExpression)))
287                 elif locationType.lower() in ['c', 'class']:
288                     element = self.wait.until(EC.visibility_of_any_elements_located((By.CLASS_NAME, locatorExpression)))
289                 elif locationType.lower() in ['l', 'link']:
290                     element = self.wait.until(EC.visibility_of_any_elements_located((By.LINK_TEXT, locatorExpression)))
291                 elif locationType.lower() in ['p', 'partial_link']:
292                     element = self.wait.until(EC.visibility_of_any_elements_located((By.PARTIAL_LINK_TEXT, locatorExpression)))
293                 elif locationType.lower() in ['t', 'tag_name']:
294                     element = self.wait.until(EC.visibility_of_any_elements_located((By.TAG_NAME, locatorExpression)))
295                 elif locationType.lower() in ['x', 'xpath']:
296                     element = self.wait.until(EC.visibility_of_any_elements_located((By.XPATH, locatorExpression)))
297                 elif locationType.lower() in ['s', 'css']:
298                     element = self.wait.until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR, locatorExpression)))
299                 return element
300         except TimeoutException as e:
301             print(traceback.print_exc())
302         except NoSuchElementException as e:
303             print(traceback.print_exc())
304         except Exception as e:
305             raise e
306 
307     def presence_of_all_elements_located(self,selector):
308         """
309         判断页面上至少有一个元素出现,如果满足条件返回所有满足定位表达式的页面元素,传入元祖类型定位器
310         """
311         try:
312             if '=>' not in selector:
313                 element = self.wait.until(EC.presence_of_all_elements_located((By.ID, selector)))
314                 return element
315             else:
316                 locationType = selector.split('=>')[0]
317                 locatorExpression = selector.split('=>')[1]
318                 if locationType.lower() in ['i', 'id']:
319                     element = self.wait.until(EC.presence_of_all_elements_located((By.ID, locatorExpression)))
320                 elif locationType.lower() in ['n', 'name']:
321                     element = self.wait.until(EC.presence_of_all_elements_located((By.NAME, locatorExpression)))
322                 elif locationType.lower() in ['c', 'class']:
323                     element = self.wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, locatorExpression)))
324                 elif locationType.lower() in ['l', 'link']:
325                     element = self.wait.until(EC.presence_of_all_elements_located((By.LINK_TEXT, locatorExpression)))
326                 elif locationType.lower() in ['p', 'partial_link']:
327                     element = self.wait.until(EC.presence_of_all_elements_located((By.PARTIAL_LINK_TEXT, locatorExpression)))
328                 elif locationType.lower() in ['t', 'tag_name']:
329                     element = self.wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, locatorExpression)))
330                 elif locationType.lower() in ['x', 'xpath']:
331                     element = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, locatorExpression)))
332                 elif locationType.lower() in ['s', 'css']:
333                     element = self.wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, locatorExpression)))
334                 return element
335         except TimeoutException as e:
336             print(traceback.print_exc())
337         except NoSuchElementException as e:
338             print(traceback.print_exc())
339         except Exception as e:
340             raise e
341 
342     def staleness_of(self,selector):
343         """
344         判断一个元素是否仍在DOM中,如果在规定的时间内已经移出,返回True,否则返回False,传入WebElement对象
345         """
346         try:
347             element = self.wait.until(EC.staleness_of((findElement(self.driver,selector))))
348             return element
349         except TimeoutException as e:
350             print(traceback.print_exc())
351         except NoSuchElementException as e:
352             print(traceback.print_exc())
353         except Exception as e:
354             raise e
355 
356     def text_to_be_present_in_element(self,selector,text):
357         """
358         判断文本内容text是否出现在某个元素中,判断的是元素的text,定位器是一个元祖
359         """
360         try:
361             if '=>' not in selector:
362                 element = self.wait.until(EC.text_to_be_present_in_element((By.ID, selector),text))
363                 return element
364             else:
365                 locationType = selector.split('=>')[0]
366                 locatorExpression = selector.split('=>')[1]
367                 if locationType.lower() in ['i', 'id']:
368                     element = self.wait.until(EC.text_to_be_present_in_element((By.ID, locatorExpression),text))
369                 elif locationType.lower() in ['n', 'name']:
370                     element = self.wait.until(EC.text_to_be_present_in_element((By.NAME, locatorExpression),text))
371                 elif locationType.lower() in ['c', 'class']:
372                     element = self.wait.until(EC.text_to_be_present_in_element((By.CLASS_NAME, locatorExpression),text))
373                 elif locationType.lower() in ['l', 'link']:
374                     element = self.wait.until(EC.text_to_be_present_in_element((By.LINK_TEXT, locatorExpression),text))
375                 elif locationType.lower() in ['p', 'partial_link']:
376                     element = self.wait.until(EC.text_to_be_present_in_element((By.PARTIAL_LINK_TEXT, locatorExpression),text))
377                 elif locationType.lower() in ['t', 'tag_name']:
378                     element = self.wait.until(EC.text_to_be_present_in_element((By.TAG_NAME, locatorExpression),text))
379                 elif locationType.lower() in ['x', 'xpath']:
380                     element = self.wait.until(EC.text_to_be_present_in_element((By.XPATH, locatorExpression),text))
381                 elif locationType.lower() in ['s', 'css']:
382                     element = self.wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, locatorExpression),text))
383                 return element
384         except TimeoutException as e:
385             print(traceback.print_exc())
386         except NoSuchElementException as e:
387             print(traceback.print_exc())
388         except Exception as e:
389             raise e
390 
391     def text_to_be_present_in_element_value(self,selector,text):
392         """
393         判断text是否出现在元素的value属性值中,定位器是一个元祖
394         """
395         try:
396             if '=>' not in selector:
397                 element = self.wait.until(EC.text_to_be_present_in_element_value((By.ID, selector),text))
398                 return element
399             else:
400                 locationType = selector.split('=>')[0]
401                 locatorExpression = selector.split('=>')[1]
402                 if locationType.lower() in ['i', 'id']:
403                     element = self.wait.until(EC.text_to_be_present_in_element_value((By.ID, locatorExpression),text))
404                 elif locationType.lower() in ['n', 'name']:
405                     element = self.wait.until(EC.text_to_be_present_in_element_value((By.NAME, locatorExpression),text))
406                 elif locationType.lower() in ['c', 'class']:
407                     element = self.wait.until(EC.text_to_be_present_in_element_value((By.CLASS_NAME, locatorExpression),text))
408                 elif locationType.lower() in ['l', 'link']:
409                     element = self.wait.until(EC.text_to_be_present_in_element_value((By.LINK_TEXT, locatorExpression),text))
410                 elif locationType.lower() in ['p', 'partial_link']:
411                     element = self.wait.until(EC.text_to_be_present_in_element_value((By.PARTIAL_LINK_TEXT, locatorExpression),text))
412                 elif locationType.lower() in ['t', 'tag_name']:
413                     element = self.wait.until(EC.text_to_be_present_in_element_value((By.TAG_NAME, locatorExpression),text))
414                 elif locationType.lower() in ['x', 'xpath']:
415                     element = self.wait.until(EC.text_to_be_present_in_element_value((By.XPATH, locatorExpression),text))
416                 elif locationType.lower() in ['s', 'css']:
417                     element = self.wait.until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR, locatorExpression),text))
418                 return element
419         except TimeoutException as e:
420             print(traceback.print_exc())
421         except NoSuchElementException as e:
422             print(traceback.print_exc())
423         except Exception as e:
424             raise e
425 
426     def new_window_is_opened(self,handles):
427         """
428         判断窗口是否打开
429         :param handles:窗口句柄, 可传self.driver.window_handles,用于click后打开新窗口后的判断
430         :return:True or False
431         """
432         return self.wait.until(EC.new_window_is_opened(handles))
433 
434 
435     def title_contains(self,partial_title):
436         """
437         判断页面title标签内容是否包含partial_title,只需要部分匹配即可,包含返回True,否则返回False
438         """
439         try:
440             return self.wait.until(EC.title_contains(partial_title))
441         except TimeoutException as e:
442             print(traceback.print_exc())
443         except NoSuchElementException as e:
444             print(traceback.print_exc())
445         except Exception as e:
446             raise e
447 
448     def title_is(self,title_text):
449         """
450         判断页面title内容是否和传入的title_text内容完全匹配,匹配返回True,否则返回False
451         """
452         try:
453             return self.wait.until(EC.title_is(title_text))
454         except:
455             return False
456 
457 
458 
459 
460 if __name__ == "__main__":
461     pass

 

posted @ 2020-04-16 23:37  此去经年归来仍是少年  阅读(299)  评论(0编辑  收藏  举报