forked from robotframework/SeleniumLibrary
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrun.py
executable file
·338 lines (307 loc) · 10.9 KB
/
run.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python
"""Script to run SeleniumLibrary acceptance tests.
Tests are executed using Robot Framework and results verified automatically
afterwards using `robotstatuschecker` tool. The tool can be installed using
`pip install robotstatuschecker` and more information about it can be found
from: https://github.com/robotframework/statuschecker/. Notice that initially
some tests may fail.
It is possible to run test with Selenium Grid. the Grid requires that Java
is available in the PATH and Grid is downloaded in the root of the repository.
The Grid jar file name should start with "selenium-server-standalone" and
this script will automatically start the Grid with hub and node roles.
More details about the Selenium grid can be found from:
https://github.com/SeleniumHQ/selenium/wiki/Grid2
When Selenium Grid is used, it is possible to include and exclude test.
Generally speaking almost all test should work when Selenium Grid is used,
but there few valid exceptions. If test uses `robotstatuschecker` tool
to verify logging of the keyword, in some cases Selenium Grid adds
some extra logging and causes test to fail. In this case, these test
should be tagged with `NoGrid` tag to exclude the test when Selenium Grid
is being used. Also there might be need write test that are only run
when Selenium Grid is used. Then in this case, test should be tagged with
`OnlyGrid` tag to include them only when Selenium Grid is used.
It is possible to pass Robot Framework command line arguments to the test
execution as last arguments to the `run_tests.py` command. It is
recommended to use arguments to select required suite or test for the
execution when developing new functionality for the library. Example like
--test, --suite, --include and --exclude.
Examples:
run.py chrome
run.py headlesschrome
run.py --interpreter c:\\Python38\\python.exe firefox --suite javascript
run.py headlesschrome --nounit --grid true
"""
import platform
import time
import zipfile
from contextlib import contextmanager
import os
import sys
import argparse
import textwrap
import shutil
import subprocess
import tempfile
from robot import rebot_cli
from robot import __version__ as robot_version
from selenium import __version__ as selenium_version
from selenium.webdriver.common.utils import free_port
from robot.utils import is_truthy
try:
import robotstatuschecker
except ImportError:
sys.exit(
"Required `robotstatuschecker` not installed.\n"
"Install it with `pip install robotstatuschecker`."
)
import requests
# Folder settings
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
ACCEPTANCE_TEST_DIR = os.path.join(ROOT_DIR, "acceptance")
UNIT_TEST_RUNNER = os.path.join(ROOT_DIR, os.pardir, "utest", "run.py")
RESOURCES_DIR = os.path.join(ROOT_DIR, "resources")
RESULTS_DIR = os.path.join(ROOT_DIR, "results")
ZIP_DIR = os.path.join(ROOT_DIR, "zip_results")
SRC_DIR = os.path.normpath(os.path.join(ROOT_DIR, os.pardir, "src"))
TEST_LIBS_DIR = os.path.join(RESOURCES_DIR, "testlibs")
HTTP_SERVER_FILE = os.path.join(RESOURCES_DIR, "testserver", "testserver.py")
EVENT_FIRING_LISTENER = os.path.join(RESOURCES_DIR, "testlibs", "MyListener.py")
ROBOT_OPTIONS = [
"--doc",
"SeleniumLibrary acceptance tests with {browser}",
"--outputdir",
RESULTS_DIR,
"--variable",
"BROWSER:{browser}",
"--report",
"NONE",
"--log",
"NONE",
"--loglevel",
"DEBUG",
"--pythonpath",
SRC_DIR,
"--pythonpath",
TEST_LIBS_DIR,
]
REBOT_OPTIONS = [
"--outputdir",
RESULTS_DIR,
]
def acceptance_tests(
interpreter, browser, rf_options=None, grid=None, event_firing=None
):
if os.path.exists(RESULTS_DIR):
shutil.rmtree(RESULTS_DIR)
os.mkdir(RESULTS_DIR)
port = free_port()
print(f"Using port: {port}")
if grid:
hub, node = start_grid()
with http_server(interpreter, port):
execute_tests(interpreter, browser, rf_options, grid, event_firing, port)
failures = process_output(browser)
if failures:
print(
"\n{} critical test{} failed.".format(
failures, "s" if failures != 1 else ""
)
)
else:
print("\nAll critical tests passed.")
if grid:
hub.kill()
node.kill()
return failures
def start_grid():
node_file = tempfile.TemporaryFile()
hub_file = tempfile.TemporaryFile()
selenium_jar = None
for file in os.listdir("."):
if file.startswith("selenium-server-standalone"):
selenium_jar = file
break
if not selenium_jar:
raise ValueError("Selenium server jar not found: %s" % selenium_jar)
hub = subprocess.Popen(
["java", "-jar", selenium_jar, "-role", "hub", "-host", "localhost"],
stderr=subprocess.STDOUT,
stdout=hub_file,
)
time.sleep(2) # It takes about two seconds to start the hub.
ready = _grid_status(False, "hub")
if not ready:
hub.kill()
raise ValueError("Selenium grid hub was not ready in 60 seconds.")
node = subprocess.Popen(
["java", "-jar", selenium_jar, "-role", "node"],
stderr=subprocess.STDOUT,
stdout=node_file,
)
ready = _grid_status(True, "node")
if not ready:
hub.kill()
node.kill()
raise ValueError("Selenium grid node was not ready in 60 seconds.")
return hub, node
def _grid_status(status=False, role="hub"):
count = 0
while True:
try:
response = requests.get("http://localhost:4444/wd/hub/status")
data = response.json()
if data["value"]["ready"] == status:
print("Selenium grid %s ready/started." % role)
return True
except Exception:
pass
count += 1
if count == 12:
raise ValueError("Selenium grid %s not ready/started in 60 seconds." % role)
print("Selenium grid %s not ready/started." % role)
time.sleep(5)
@contextmanager
def http_server(interpreter, port:int):
serverlog = open(os.path.join(RESULTS_DIR, "serverlog.txt"), "w")
interpreter = "python" if not interpreter else interpreter
process = subprocess.Popen(
[interpreter, HTTP_SERVER_FILE, "start", "--port", str(port)],
stdout=serverlog,
stderr=subprocess.STDOUT,
)
try:
yield
finally:
subprocess.call([interpreter, HTTP_SERVER_FILE, "stop", "--port", str(port)])
process.wait()
serverlog.close()
def execute_tests(interpreter, browser, rf_options, grid, event_firing, port):
options = []
if grid:
runner = interpreter.split() + [
"-m",
"pabot.pabot",
"--processes",
"2",
]
else:
runner = interpreter.split() + ["-m", "robot.run", "--variable", f"PORT:{port}"]
if platform.system() == "Darwin":
runner.append("--exclude")
runner.append("SKIP_ON_MAC")
options.extend([opt.format(browser=browser) for opt in ROBOT_OPTIONS])
if rf_options:
options += rf_options
options += ["--exclude", f"known issue {browser.replace('headless', '')}", "--exclude", "triage"]
command = runner
if grid:
command += [
"--variable",
"REMOTE_URL:http://localhost:4444/wd/hub",
"--exclude",
"NoGrid",
]
else:
command += ["--exclude", "OnlyGrid"]
if event_firing:
command += [
"--variable",
f"event_firing_or_none:{EVENT_FIRING_LISTENER}",
]
command += options + [ACCEPTANCE_TEST_DIR]
log_start(command)
syslog = os.path.join(RESULTS_DIR, "syslog.txt")
subprocess.call(command, env=dict(os.environ, ROBOT_SYSLOG_FILE=syslog))
def log_start(command_list, *hiddens):
command = subprocess.list2cmdline(command_list)
for hidden in hiddens:
if hidden:
command = command.replace(hidden, "*" * len(hidden))
print()
print("Starting test execution with command:")
print(command)
def process_output(browser):
print("Verifying results...")
options = []
output = os.path.join(RESULTS_DIR, "output.xml")
robotstatuschecker.process_output(output)
options.extend([opt.format(browser=browser) for opt in REBOT_OPTIONS])
try:
rebot_cli(options + [output])
except SystemExit as exit:
return exit.code
def create_zip(browser = None):
if os.path.exists(ZIP_DIR):
shutil.rmtree(ZIP_DIR)
os.mkdir(ZIP_DIR)
python_version = platform.python_version()
zip_name = f"rf-{robot_version}-python-{python_version}-selenium-{selenium_version}-{browser}.zip"
zip_path = os.path.join(ZIP_DIR, zip_name)
print("Zip created in: %s" % zip_path)
zip_file = zipfile.ZipFile(zip_path, "a")
for root, dirs, files in os.walk(RESULTS_DIR):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.join("SeleniumLibrary/atest/result", file)
zip_file.write(file_path, arcname)
zip_file.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__.splitlines()[0],
formatter_class=argparse.RawTextHelpFormatter,
epilog="\n".join(__doc__.splitlines()[2:]),
)
parser.add_argument(
"--interpreter",
"-I",
default=sys.executable,
help=textwrap.dedent(
"""\
Any Python interpreter supported by the library.
E.g. `python` or `c:\\Python38\\python.exe`.
By default set to `python`."""
),
)
parser.add_argument(
"browser",
help=("Any browser supported by the library (e.g. `chrome`or `firefox`)."),
)
parser.add_argument(
"--nounit",
help="Does not run unit test when set.",
default=False,
action="store_true",
)
parser.add_argument(
"--grid", "-G", help="Run test by using Selenium grid", default=False
)
parser.add_argument(
"--zip",
help="Creates zip file from output dir.",
default=False,
action="store_true",
)
parser.add_argument(
"--event-firing-webdriver",
help="Run tests using event firing webdriver.",
default=False,
action="store_true",
)
args, rf_options = parser.parse_known_args()
browser = args.browser.lower().strip()
selenium_grid = is_truthy(args.grid)
interpreter = "python" if not args.interpreter else args.interpreter
event_firing_webdriver = args.event_firing_webdriver
if args.nounit:
print("Not running unit tests.")
else:
rc = subprocess.call([interpreter, UNIT_TEST_RUNNER])
if rc != 0:
print("Not running acceptance test, because unit tests failed.")
sys.exit(rc)
failures = acceptance_tests(
interpreter, browser, rf_options, selenium_grid, event_firing_webdriver
)
if args.zip:
create_zip(browser)
sys.exit(failures)