zulip/frontend_tests/run-casper

132 lines
4.7 KiB
Python
Executable File

#!/usr/bin/env python
from __future__ import print_function
import optparse
import subprocess
import sys
import os
import glob
try:
# We don't actually need typing, but it's a good guard for being
# outside a Zulip virtualenv.
from typing import Iterable
except ImportError as e:
print("ImportError: {}".format(e))
print("You need to run the Zulip tests inside a Zulip dev environment.")
print("If you are using Vagrant, you can `vagrant ssh` to enter the Vagrant guest.")
sys.exit(1)
#
# In order to use remote casperjs debugging, pass the --remote-debug flag
# This will start a remote debugging session listening on port 7777
#
# See https://wiki.zulip.net/wiki/Testing_the_app for more information
# on how to use remote debugging
#
os.environ["CASPER_TESTS"] = "1"
os.environ["PHANTOMJS_EXECUTABLE"] = os.path.join(os.path.dirname(__file__), "../node_modules/.bin/phantomjs")
usage = """%prog [options]
test-js-with-casper # Run all test files
test-js-with-casper 09-navigation.js # Run a single test file
test-js-with-casper 09 # Run a single test file 09-navigation.js
test-js-with-casper 01-login.js 03-narrow.js # Run a few test files
test-js-with-casper 01 03 # Run a few test files, 01-login.js and 03-narrow.js here"""
parser = optparse.OptionParser(usage)
parser.add_option('--skip-flaky-tests', dest='skip_flaky',
action="store_true",
default=False, help='Skip flaky tests')
parser.add_option('--force', dest='force',
action="store_true",
default=False, help='Run tests despite possible problems.')
parser.add_option('--remote-debug',
help='Whether or not to enable remote debugging on port 7777',
action="store_true",
default=False)
(options, args) = parser.parse_args()
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(TOOLS_DIR))
from tools.lib.test_script import get_provisioning_status
from tools.lib.test_server import test_server_running
if not options.force:
ok, msg = get_provisioning_status()
if not ok:
print(msg)
print('If you really know what you are doing, use --force to run anyway.')
sys.exit(1)
os.chdir(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
subprocess.check_call('tools/setup/generate-test-credentials')
subprocess.check_call(['mkdir', '-p', 'var/casper'])
subprocess.check_call(['rm', '-f'] + glob.glob('var/casper/casper-failure*.png'))
LOG_FILE = 'var/casper/server.log'
if os.path.exists(LOG_FILE) and os.path.getsize(LOG_FILE) < 100000:
log = open(LOG_FILE, 'a')
log.write('\n\n')
else:
log = open(LOG_FILE, 'w')
def run_tests(realms_have_subdomains, files, external_host):
# type: (bool, Iterable[str], str) -> None
test_dir = os.path.join(os.path.dirname(__file__), '../frontend_tests/casper_tests')
test_files = []
for file in files:
for file_name in os.listdir(test_dir):
if file_name.startswith(file):
file = file_name
break
if not os.path.exists(file):
file = os.path.join(test_dir, file)
test_files.append(os.path.abspath(file))
if not test_files:
test_files = sorted(glob.glob(os.path.join(test_dir, '*.js')))
# 10-admin.js is too flaky!
if options.skip_flaky:
test_files = [fn for fn in test_files if '10-admin' not in fn]
remote_debug = ""
if options.remote_debug:
remote_debug = "--remote-debugger-port=7777 --remote-debugger-autorun=yes"
with test_server_running(options.force, external_host, log, dots=True):
ret = 1
for test_file in test_files:
cmd = "node_modules/.bin/casperjs %s test --subdomains=%s %s" % (
remote_debug, realms_have_subdomains, test_file)
print("\n\nRunning %s" % (cmd,))
ret = subprocess.call(cmd, shell=True)
if ret != 0:
break
if ret != 0:
print("""
Oops, the frontend tests failed. Tips for debugging:
* Check the frontend test server logs at %s
* Check the screenshots of failed tests at var/casper/casper-failure*.png
* Try remote debugging the test web browser as described in docs/testing-with-casper.md
""" % (LOG_FILE,), file=sys.stderr)
sys.exit(ret)
external_host = "localhost:9981"
# First, run all tests with REALMS_HAVE_SUBDOMAINS set to False
run_tests(False, args, external_host)
# Now run a subset of the tests with REALMS_HAVE_SUBDOMAINS set to True
os.environ["REALMS_HAVE_SUBDOMAINS"] = "True"
external_host = "zulipdev.com:9981"
if len(args) == 0:
run_tests(True, ["00-realm-creation.js", "01-login.js", "02-site.js"], external_host)
else:
run_tests(True, args, external_host)
sys.exit(0)