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