forked from web-animations/web-animations-js-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-tests.py
594 lines (483 loc) · 19.8 KB
/
run-tests.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/usr/bin/python
#
# -*- coding: utf-8 -*-
# vim: set ts=4 sw=4 et sts=4 ai:
import atexit
import json as simplejson
import os
import platform
import pprint
import re
import socket
import sys
import time
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"-b", "--browser", type=str, required=True,
choices=['Firefox', 'Chrome', 'Ie', 'PhantomJS', 'Remote'],
help="Which WebDriver to use.")
parser.add_argument(
"-x", "--virtual", action='store_true', default=False,
help="Use a virtual screen system such as Xvfb, Xephyr or Xvnc.")
parser.add_argument(
"-d", "--dontexit", action='store_true', default=False,
help="At end of testing, don't exit.")
parser.add_argument(
"-a", "--auto-install", action='store_true', default=True,
help="Auto install any dependencies.")
parser.add_argument(
"-v", "--verbose", action='store_true', default=False,
help="Output more information.")
parser.add_argument(
"-u", "--upload", action='store_true', default=False,
help="Upload images to picture sharing site (http://postimage.org/),"
" only really useful for testbots.")
# Only used by the Remote browser option.
parser.add_argument(
"--remote-executor", type=str,
help="Location of the Remote executor.")
parser.add_argument(
"--remote-caps", action='append',
help="Location of capabilities to request on Remote executor.",
default=[])
parser.add_argument(
"-s", "--sauce", action='store_true', default=False,
help="Use the SauceLab's Selenium farm rather then locally starting"
" selenium. SAUCE_USERNAME and SAUCE_ACCESS_KEY must be set in"
" environment.")
# Subunit / testrepository support
parser.add_argument(
"--subunit", action='store_true', default=False,
help="Output raw subunit binary data.")
parser.add_argument(
"--list", action='store_true', default=False,
help="List tests which are avalible.")
parser.add_argument(
"--load-list", type=argparse.FileType('r'),
help="List of tests to run.")
args = parser.parse_args()
if args.verbose and args.subunit:
raise SystemExit("--verbose and --subunit are not compatible.")
# Make sure the repository is setup and the dependencies exist
# -----------------------------------------------------------------------------
import subprocess
# Set up the git repository
if args.auto_install:
subprocess.check_call(["git", "submodule", "init"])
subprocess.check_call(["git", "submodule", "update"])
# Install the python modules
def autoinstall(name, package=None):
if not package:
package = name
if args.auto_install:
try:
import pip
except ImportError:
raise SystemExit("""\
Can not autoinstall as PIP is not avaliable.
To install 'pip' please ask your administrator to install the package
'python-pip' or run:
# sudo apt-get install python-pip
""")
from pip.req import InstallRequirement
install = InstallRequirement(package, None)
install.check_if_exists()
if install.satisfied_by is None:
print "Unable to find %s, autoinstalling" % (name,)
if install.conflicts_with:
raise SystemExit("""
Can't install %s because it conflicts with already installed %s.
Please try installing %s manually with:
# sudo pip install --upgrade %s
""" % (name, install.conflicts_with, name, package.replace(">", "\>")))
ret = subprocess.call(["pip", "install", "--user", package])
if ret == 0: # UNKNOWN_ERROR
# Restart python is a nasty way, only method to get imports to refresh.
import sys, os
python = sys.executable
os.execl(python, python, *sys.argv)
else:
raise SystemExit("""
Unknown error occurred.
Please install the Python %s module.
# sudo pip install %s
""" % (name, package.replace(">", "\n")))
for line in file(".requirements").readlines():
if line.startswith('#') or not line.strip():
continue
if line.find('#') >= 0:
package, name = line.split('#')
autoinstall(name.strip(), package.strip())
else:
autoinstall(line.strip())
if not args.sauce:
# Get any selenium drivers we might need
if args.browser == "Chrome":
# Get ChromeDriver if it's not in the path...
# https://code.google.com/p/chromedriver/downloads/list
chromedriver_bin = None
if platform.system() == "Linux":
chromedriver_bin = "chromedriver"
if platform.processor() == "x86_64":
# 64 bit binary needed
chromedriver_url = "https://chromedriver.googlecode.com/files/chromedriver2_linux64_0.6.zip"
else:
# 32 bit binary needed
chromedriver_url = "https://chromedriver.googlecode.com/files/chromedriver_linux32_26.0.1383.0.zip"
elif platform.system() == "mac":
chromedriver_url = "https://chromedriver.googlecode.com/files/chromedriver2_mac32_0.7.zip"
chromedriver_bin = "chromedriver"
elif platform.system() == "win32":
chromedriver_bin = "https://chromedriver.googlecode.com/files/chromedriver2_win32_0.7.zip"
chromedriver_url = "chromedriver.exe"
try:
if subprocess.call(chromedriver_bin) != 0:
raise OSError("Return code?")
except OSError:
chromedriver_local = os.path.join("tools", chromedriver_bin)
if not os.path.exists(chromedriver_local):
import urllib2, zipfile
import cStringIO as StringIO
datafile = StringIO.StringIO(urllib2.urlopen(chromedriver_url).read())
contents = zipfile.ZipFile(datafile, 'r')
contents.extract(chromedriver_bin, "tools")
chromedriver = os.path.realpath(chromedriver_local)
os.chmod(chromedriver, 0755)
else:
chromedriver = "chromedriver"
elif args.browser == "Firefox":
pass
elif args.browser == "PhantomJS":
phantomjs_bin = None
if platform.system() == "Linux":
phantomjs_bin = "phantomjs"
if platform.processor() == "x86_64":
# 64 bit binary needed
phantomjs_url = "https://phantomjs.googlecode.com/files/phantomjs-1.9.0-linux-x86_64.tar.bz2"
else:
# 32 bit binary needed
phantomjs_url = "https://phantomjs.googlecode.com/files/phantomjs-1.9.0-linux-i686.tar.bz2"
phantomjs_local = os.path.join("tools", phantomjs_bin)
if not os.path.exists(phantomjs_local):
import urllib2, tarfile
import cStringIO as StringIO
datafile = StringIO.StringIO(urllib2.urlopen(phantomjs_url).read())
contents = tarfile.TarFile.open(fileobj=datafile, mode='r:bz2')
file("tools/"+phantomjs_bin, "w").write(
contents.extractfile("phantomjs-1.9.0-linux-x86_64/bin/"+phantomjs_bin).read())
phantomjs = os.path.realpath(phantomjs_local)
os.chmod(phantomjs, 0755)
else:
if platform.system() == "mac":
phantomjs_url = "https://phantomjs.googlecode.com/files/phantomjs-1.9.0-macosx.zip"
phantomjs_bin = "phantomjs"
elif platform.system() == "win32":
chromedriver_bin = "https://phantomjs.googlecode.com/files/phantomjs-1.9.0-windows.zip"
phantomjs_url = "phantomjs.exe"
phantomjs_local = os.path.join("tools", phantomjs_bin)
if not os.path.exists(phantomjs_local):
import urllib2, zipfile
import cStringIO as StringIO
datafile = StringIO.StringIO(urllib2.urlopen(phantomjs_url).read())
contents = zipfile.ZipFile(datafile, 'r')
contents.extract(phantomjs_bin, "tools")
phantomjs = os.path.realpath(phantomjs_local)
os.chmod(phantomjs, 0755)
else:
assert os.environ['SAUCE_USERNAME']
assert os.environ['SAUCE_ACCESS_KEY']
username = os.environ['SAUCE_USERNAME']
key = os.environ['SAUCE_ACCESS_KEY']
# Download the Sauce Connect script
sauce_connect_url = "http://saucelabs.com/downloads/Sauce-Connect-latest.zip"
sauce_connect_bin = "Sauce-Connect.jar"
sauce_connect_local = os.path.join("tools", sauce_connect_bin)
if not os.path.exists(sauce_connect_local):
import urllib2, zipfile
import cStringIO as StringIO
datafile = StringIO.StringIO(urllib2.urlopen(sauce_connect_url).read())
contents = zipfile.ZipFile(datafile, 'r')
contents.extract(sauce_connect_bin, "tools")
if 'TRAVIS_JOB_NUMBER' in os.environ:
tunnel_id = os.environ['TRAVIS_JOB_NUMBER']
else:
tunnel_id = "%s:%s" % (socket.gethostname(), os.getpid())
args.remote_caps.append('tunnel-identifier=%s' % tunnel_id)
# Kill the tunnel when we die
def kill_tunnel(sauce_tunnel):
if sauce_tunnel.returncode is None:
sauce_tunnel.terminate()
timeout = time.time()
while sauce_tunnel.poll() is None:
if time.time() - timeout < 30:
time.sleep(1)
else:
sauce_tunnel.kill()
readyfile = "."+tunnel_id
sauce_tunnel = None
try:
sauce_log = file("sauce_tunnel.log", "w")
sauce_tunnel = subprocess.Popen([
"java", "-jar", sauce_connect_local,
"--readyfile", readyfile,
"--tunnel-identifier", tunnel_id,
username, key,
],
stdout=sauce_log, stderr=sauce_log,
)
atexit.register(kill_tunnel, sauce_tunnel)
# Wait for the tunnel to come up
while not os.path.exists(readyfile):
time.sleep(0.5)
except:
if sauce_tunnel:
kill_tunnel(sauce_tunnel)
args.remote_executor = "http://%s:%s@localhost:4445/wd/hub" % (username, key)
# Send travis information upstream
if 'TRAVIS_BUILD_NUMBER' in os.environ:
args.remote_caps.append('build=%s' % os.environ['TRAVIS_BUILD_NUMBER'])
# -----------------------------------------------------------------------------
import subunit, testtools, unittest
if args.list:
for test in simplejson.loads(
file("test/testcases.js").read()[len("testCases("):-(len(");")+1)]):
print test[:-5]
sys.exit(-1)
if args.load_list:
tests = args.load_list.readlines()
else:
tests = []
# Collect summary of all the individual test runs
summary = testtools.StreamSummary()
# Output information to stdout
if not args.subunit:
# Human readable test output
pertest = testtools.StreamToExtendedDecorator(
testtools.MultiTestResult(
# Individual test progress
unittest.TextTestResult(
unittest.runner._WritelnDecorator(sys.stdout), False, 2),
# End of run, summary of failures.
testtools.TextTestResult(sys.stdout),
))
else:
from subunit.v2 import StreamResultToBytes
pertest = StreamResultToBytes(sys.stdout)
output = subunit.CopyStreamResult([summary, pertest])
output.startTestRun()
# Start up a local HTTP server which serves the files to the browser and
# collects the test results.
# -----------------------------------------------------------------------------
import SimpleHTTPServer
import SocketServer
import threading
import cgi
import re
import itertools
import mimetools
import mimetypes
import urllib, urllib2
class MultiPartForm(object):
"""Accumulate the data to be used when posting a form."""
def __init__(self):
self.form_fields = []
self.files = []
self.boundary = mimetools.choose_boundary()
return
def get_content_type(self):
return 'multipart/form-data; boundary=%s' % self.boundary
def add_field(self, name, value):
"""Add a simple field to the form data."""
self.form_fields.append((name, value))
return
def add_file(self, fieldname, filename, fileHandle, mimetype=None):
"""Add a file to be uploaded."""
body = fileHandle.read()
if mimetype is None:
mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
self.files.append((fieldname, filename, mimetype, body))
return
def __str__(self):
"""Return a string representing the form data, including attached files."""
# Build a list of lists, each containing "lines" of the
# request. Each part is separated by a boundary string.
# Once the list is built, return a string where each
# line is separated by '\r\n'.
parts = []
part_boundary = '--' + self.boundary
# Add the form fields
parts.extend(
[ part_boundary,
'Content-Disposition: form-data; name="%s"' % name,
'',
value,
]
for name, value in self.form_fields
)
# Add the files to upload
parts.extend(
[ part_boundary,
'Content-Disposition: file; name="%s"; filename="%s"' % \
(field_name, filename),
'Content-Type: %s' % content_type,
'',
body,
]
for field_name, filename, content_type, body in self.files
)
# Flatten the list and add closing boundary marker,
# then return CR+LF separated data
flattened = list(str(b) for b in itertools.chain(*parts))
flattened.append('--' + self.boundary + '--')
flattened.append('')
return '\r\n'.join(flattened)
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
STATUS = {0:'success', 1:'fail', 2:'fail', 3:'skip'}
# Make the HTTP requests be quiet
def log_message(self, format, *a):
if args.verbose:
SimpleHTTPServer.SimpleHTTPRequestHandler.log_message(self, format, *a)
def do_POST(self):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
data = simplejson.loads(form.getvalue('data'))
overall_status = 0
for result in data['results']:
info = dict(result)
info.pop('_structured_clone', None)
overall_status += result['status']
output.status(
test_id="%s.%s" % (data['testName'][:-5], result['name']),
test_status=self.STATUS[result['status']],
test_tags=[args.browser],
file_name='message',
file_bytes=result['message'],
mime_type='text/plain; charset=UTF-8',
eof=True)
if overall_status > 0 and args.virtual:
screenshot = data['testName'] + '.png'
disp.grab().save(screenshot)
if args.upload:
form = MultiPartForm()
form.add_field('adult', 'no')
form.add_field('optsize', '0')
form.add_file('upload[]', screenshot, fileHandle=open(screenshot, 'rb'))
request = urllib2.Request("http://postimage.org/")
body = str(form)
request.add_header('Content-type', form.get_content_type())
request.add_header('Content-length', len(body))
request.add_data(body)
result = urllib2.urlopen(request).read()
print "Screenshot at:", re.findall("""<td><textarea wrap='off' onmouseover='this.focus\(\)' onfocus='this.select\(\)' id="code_1" scrolling="no">([^<]*)</textarea></td>""", result)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
httpd = SocketServer.TCPServer(
("127.0.0.1", 0), # Bind to any port on localhost
ServerHandler)
httpd_thread = threading.Thread(target=httpd.serve_forever)
httpd_thread.daemon = True
httpd_thread.start()
port = httpd.socket.getsockname()[-1]
# Start up a virtual display, useful for testing on headless servers.
# -----------------------------------------------------------------------------
# PhantomJS doesn't need a display
disp = None
if args.virtual and args.browser != "PhantomJS":
from pyvirtualdisplay.smartdisplay import SmartDisplay
try:
disp = SmartDisplay(visible=0, bgcolor='black').start()
atexit.register(disp.stop)
except:
if disp:
disp.stop()
# Start up the web browser and run the tests.
# ----------------------------------------------------------------------------
from selenium import webdriver
driver_arguments = {}
if args.browser == "Chrome":
import tempfile, shutil
# We reference shutil to make sure it isn't garbaged collected before we use
# it.
def directory_cleanup(directory, shutil=shutil):
try:
shutil.rmtree(directory)
except OSError, e:
pass
try:
user_data_dir = tempfile.mkdtemp()
atexit.register(directory_cleanup, user_data_dir)
except:
directory_cleanup(user_data_dir)
raise
driver_arguments['chrome_options'] = webdriver.ChromeOptions()
webdriver.ChromeOptions.__repr__ = lambda self: str(self.__dict__) # Make printable
driver_arguments['chrome_options'].add_argument('--user-data-dir=%s' % user_data_dir)
driver_arguments['chrome_options'].add_argument('--enable-logging')
driver_arguments['chrome_options'].binary_location = '/usr/bin/google-chrome'
driver_arguments['executable_path'] = chromedriver
# Travis-CI uses OpenVZ containers which are incompatible with the sandbox
# technology.
# See https://code.google.com/p/chromium/issues/detail?id=31077 for more
# information.
if 'TRAVIS' in os.environ:
driver_arguments['chrome_options'].add_argument('--no-sandbox')
elif args.browser == "Firefox":
driver_arguments['firefox_profile'] = webdriver.FirefoxProfile()
elif args.browser == "PhantomJS":
driver_arguments['executable_path'] = phantomjs
driver_arguments['service_args'] = ['--remote-debugger-port=9000']
elif args.browser == "Remote":
driver_arguments['command_executor'] = args.remote_executor
caps = {}
for arg in args.remote_caps:
if not arg.strip():
continue
if arg.find('=') < 0:
caps.update(getattr(webdriver.DesiredCapabilities, arg.strip().upper()))
else:
key, value = arg.split('=', 1)
caps[key] = value
driver_arguments['desired_capabilities'] = caps
try:
browser = None
try:
if args.verbose:
print driver_arguments
browser = getattr(webdriver, args.browser)(**driver_arguments)
atexit.register(browser.close)
except:
if browser:
browser.close()
raise
url = 'http://localhost:%i/test/test-runner.html' % port
browser.get(url)
def close_other_windows(browser, url):
for win in browser.window_handles:
browser.switch_to_window(win)
if browser.current_url != url:
browser.close()
browser.switch_to_window(browser.window_handles[0])
while True:
# Sometimes other windows are accidently opened (such as an extension
# popup), close them.
if len(browser.window_handles) > 1:
close_other_windows(browser, url)
if not browser.execute_script('return window.finished'):
time.sleep(1)
continue
else:
break
finally:
output.stopTestRun()
if args.browser == "Chrome":
shutil.copy(os.path.join(user_data_dir, "chrome_debug.log"), ".")
while args.dontexit and browser.window_handles:
time.sleep(1)
if summary.wasSuccessful():
sys.exit(0)
else:
sys.exit(1)