forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.py
More file actions
149 lines (127 loc) · 5.88 KB
/
shared.py
File metadata and controls
149 lines (127 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#!/usr/bin/python
# coding: latin-1
from selenium.selenium import selenium
from selenium.common.exceptions import NoSuchElementException
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import re, sys, time, traceback
def try_except_decor(func):
def try_except(*args, **kwargs):
try:
return func(*args, **kwargs)
except WebDriverException as err:
exc_type, exc_value, exc_traceback = sys.exc_info()
print "WebDriver error. Function: {0}, error: {1}".format(func.func_code, err)
print repr(traceback.format_exception(exc_type, exc_value,exc_traceback))
except NoSuchElementException as err:
exc_type, exc_value, exc_traceback = sys.exc_info()
print "Element error. Function: {0}, error: {1}".format(func.func_code, err)
print repr(traceback.format_exception(exc_type, exc_value,exc_traceback))
except TimeoutException as err:
exc_type, exc_value, exc_traceback = sys.exc_info()
print "Timeout error. Function: {0}, error: {1}".format(func.func_code, err)
print repr(traceback.format_exception(exc_type, exc_value,exc_traceback))
return try_except
class Shared(object):
@staticmethod
@try_except_decor
def option_selection(browser, element_type, element_name, option_text, wait_element_type = '', wait_element_name = ''):
ret = False
Shared.wait_for_element(browser, element_type, element_name)
if element_type == 'id':
ele = browser.find_element_by_id(element_name)
elif element_type == 'class_name':
ele = browser.find_element_by_class_name(element_name)
options = ele.find_elements_by_tag_name('option')
option_names = [option.text for option in options]
if option_text not in option_names:
return ret
for option in options:
if option.text.find(option_text) > -1:
option.click()
ret = True
time.sleep(1)
break
if len(wait_element_type) > 0 and len(wait_element_name) > 0:
Shared.wait_for_element(browser, wait_element_type, wait_element_name)
return ret
@staticmethod
@try_except_decor
def flash_message(browser):
try:
ele1 = browser.find_element_by_id('flashMessageArea')
except NoSuchElementException:
ele1 = None
if ele1 != None:
ele2 = ele1.find_element_by_class_name('flash_message')
if ele2 != None and ele2.text != None and len(ele2.text) > 0:
return ele2.text
else:
return ''
else:
return ''
@staticmethod
@try_except_decor
def string_selection(browser, key, value, index = 0):
element = browser.find_elements_by_id(key)[index]
element.clear()
element.send_keys(value)
@staticmethod
def wait_until_title_text(browser, text, waittime = 30):
wait = WebDriverWait(browser, waittime)
wait.until(lambda browser: browser.title.lower().find(text.lower()) > -1)
@staticmethod
def wait_until_find_id(browser, element_id, waittime = 10):
wait = WebDriverWait(browser, waittime)
wait.until(lambda browser: browser.find_element_by_id(element_id))
@staticmethod
# the name should exist in the newer page, but not in older one
def wait_for_element(browser, element_type, name, waittime = 30):
wait = WebDriverWait(browser, waittime)
if element_type.lower() == 'id':
wait.until(EC.presence_of_element_located((By.ID, name)))
elif element_type.lower() == 'tag_name':
wait.until(EC.presence_of_element_located((By.TAG_NAME, name)))
elif element_type.lower() == 'class_name':
wait.until(EC.presence_of_element_located((By.CLASS_NAME, name)))
elif element_type.lower() == 'xpath':
wait.until(EC.presence_of_element_located((By.XPATH, name)))
elif element_type.lower() == 'link_text':
wait.until(EC.presence_of_element_located((By.LINK_TEXT, name)))
#feed the string through directly
else:
wait.until(EC.presence_of_element_located(element_type, name))
time.sleep(1)
def playing_around(self):
from threading import Timer
t = Timer(20,self.wait_for_invisible)
t.start()
@staticmethod
#wait until something disappears
def wait_for_invisible(browser, element_type, name, waittime=30):
wait = WebDriverWait(browser, waittime)
# the code base uses underscores, but the real string doesn't have em.
final_type = re.sub('_',' ',element_type)
wait.until(EC.invisibility_of_element_located((final_type, name)))
#this method isn't as slick as I hoped :(
time.sleep(1)