1 import os
2 import socket
3 import subprocess
4 import logging
5 import json
6 import tempfile
7 from glob import glob
8 from requests.models import Response
9
10
18
19
21 """
22 Returns tlib's absolute path
23 """
24 file_folder = os.path.dirname(__file__)
25 return os.path.abspath(os.path.join(file_folder, os.pardir))
26
27
29 """
30 Returns absolute path of tlib's asset folder
31 """
32 return os.path.abspath(os.path.join(tlib_folder(), "asset"))
33
34
36 """
37 Returns absolute path of folder containing all modules
38 """
39 return os.path.abspath(os.path.join(tlib_folder(), "base"))
40
41
43 """
44 Returns absolute path of tlib's config folder
45 """
46 return os.path.abspath(os.path.join(tlib_folder(), "config"))
47
48
50 """
51 Returns absolute path of tlib's template folder\n
52 Template folder contains jinja templates used for generation of reports\n
53 like screenshots
54 """
55 return os.path.abspath(os.path.join(tlib_folder(), "templates"))
56
57
59 """
60 Returns path to Chrome WebDriver executable
61 """
62 if version == "latest":
63
64 path = os.path.join(tlib_asset_folder(), "selenium", "chromedriver_*.exe")
65 r = sorted(glob(path))
66 return os.path.join(tlib_asset_folder(), "selenium", r[-1])
67 else:
68 return os.path.join(tlib_asset_folder(), "selenium", "chromedriver_%s.exe" % version)
69
70
76
77
79 """
80 Returns path to IE WebDriver executable
81 """
82 return os.path.join(tlib_asset_folder(), "selenium", "IE", "2.41.0", "Win32", "IEDriverServer.exe")
83
84
86 """
87 Returns path to IE WebDriver executable
88 """
89 return os.path.join(tlib_asset_folder(), "selenium", "IE", "2.41.0", "x64", "IEDriverServer.exe")
90
91
93 """
94 Returns path to Webdriver for android
95 """
96 return os.path.join(tlib_asset_folder(), "selenium", "android-server-2.21.0.apk")
97
98
100 """
101 Returns path to selendroid jar for android native app
102 """
103 return os.path.join(tlib_asset_folder(), "selenium", "selendroid-standalone-0.9.0-with-dependencies.jar")
104
105
107 """
108 Returns true if IP parameter is a valid IP address.\n
109 Currently IPs in this format are valid:\n
110 X.X.X.X\n
111 X.X.X.X:PORT
112
113 @type ip: str
114 @return: bool
115 """
116
117 if ip is None:
118 return False
119
120 try:
121 (ip, port) = ip.split(":", 1)
122 except ValueError:
123
124 port = None
125
126 try:
127 socket.inet_aton(ip)
128 except socket.error:
129 return False
130
131 if port is not None:
132 try:
133 return (int(port) >= 1) and (int(port) <= 65535)
134 except ValueError:
135
136 return False
137
138 return True
139
140
141 -def run_command(logger, command, shell=False, fail_on_error=True):
142 """
143 Run a command and skip test if exit code is not 0
144
145 Example:
146 Run 'adb devices' and don't skip test if commands returns non-zero status
147 run_command(logger, ["adb", "devices"], fail_on_error=False)
148
149 @param logger: logger for debugging purposes
150 @type logger: logging.Logger
151 @param command: Command to run
152 @type command: list
153 @param fail_on_error: When True, skip test if command returned a non-zero exit code
154 @type fail_on_error: bool
155 @rtype: list
156 @return: Returns a list with stdout and stderr output
157 """
158
159 fd_out = tempfile.NamedTemporaryFile(delete=True)
160
161 fd_err = tempfile.NamedTemporaryFile(delete=True)
162 try:
163 process = subprocess.Popen(command, shell=shell, stdout=fd_out, stderr=fd_err)
164 except WindowsError as e:
165 logger.error(r"Problem running command, skipping test.\n%s" % e)
166
167 if fail_on_error:
168 raise RuntimeError(r"Problem running command, skipping test.\n%s" % e)
169
170
171 process.communicate()
172 fd_out.seek(0)
173 fd_err.seek(0)
174 out = (fd_out.read(), fd_err.read())
175 fd_out.close()
176 fd_err.close()
177 errcode = process.returncode
178
179 if (errcode != 0) and fail_on_error:
180 logger.error(r"Program exited with code {errcode}, skipping test\n{out}".format(errcode=errcode, out=out))
181
182 raise RuntimeError(r"Program exited with code {errcode}, skipping test\n{out}".format(errcode=errcode, out=out))
183
184 return out
185
186
188 """
189 Run a command and skip test if exit code is not 0
190
191 Example:
192 Run 'adb devices' and don't skip test if commands returns non-zero status
193 run_command(logger, ["adb", "devices"], fail_on_error=False)
194
195 @param logger: logger for debugging purposes
196 @type logger: logging.Logger
197 @param command: Command to run
198 @type command: list
199 @rtype: list
200 @return: Returns a list with stdout and stderr output
201 """
202
203 try:
204 process = subprocess.Popen(command, shell=shell)
205 return process
206 except WindowsError as e:
207 logger.error(r"Problem running command, skipping test.\n%s" % e)
208
209
210
212 """
213 Sorts a list, and if the list has other objects inside, it will iterate over them
214 @param l: list to sort
215 @type l: list
216 @return: sorted list
217 @rtype: list
218 """
219 if type(l) is not list:
220 raise RuntimeError("Parameter l is not a list")
221
222 for i, s in enumerate(l):
223 if type(s) is list:
224 l[i] = sort_list(s)
225 if type(s) is dict:
226 l[i] = sort_dict(s)
227
228 l.sort()
229
230 return l
231
232
234 """
235 Sorts a dictionary, and if the list has other objects inside, it will iterate over them
236 @param d: dictionary to sort
237 @type d: dict
238 @return: sorted dictionary
239 @rtype: dict
240 """
241 if type(d) is not dict:
242 raise RuntimeError("Parameter d is not a dictionary")
243
244 for key in d.keys():
245 if type(d[key]) is list:
246
247 d[key] = sort_list(d[key])
248 elif type(d[key]) is dict:
249 d[key] = sort_dict(d[key])
250
251 return d
252
253
255 """
256 Logs REST request and response
257
258 :param response: Object returned by requests class
259 :param logger: Where to log request and response
260 :type logger: logging.Logger
261 """
262
263 logger.debug("Request\n\t{url} [{status_code} - {reason}]".
264 format(url=response.request.url, status_code=response.status_code, reason=response.reason))
265
266 try:
267
268 json_resp = response.json()
269 logger.debug("Response\n" + json.dumps(json_resp, sort_keys=True, indent=4, separators=(',', ': ')))
270 return
271 except ValueError:
272
273 pass
274
275
276 logger.debug("Response\n" + response.content)
277