2020-03-16 13:44:15 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import shlex
|
2020-06-11 00:54:34 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-03-16 13:44:15 +01:00
|
|
|
|
2021-03-24 12:25:30 +01:00
|
|
|
import requests
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
|
2020-11-28 19:27:44 +01:00
|
|
|
from scripts.lib.zulip_tools import ENDC, FAIL, OKGREEN
|
|
|
|
|
2020-03-16 13:44:15 +01:00
|
|
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
2021-03-24 12:25:30 +01:00
|
|
|
from zerver.lib.test_fixtures import reset_zulip_test_database
|
2020-03-16 13:44:15 +01:00
|
|
|
|
2020-05-09 22:26:43 +02:00
|
|
|
# Request the special webpack setup for frontend integration tests,
|
|
|
|
# where webpack assets are compiled up front rather than running in
|
2020-09-05 15:37:07 +02:00
|
|
|
# watch mode.
|
|
|
|
os.environ["PUPPETEER_TESTS"] = "1"
|
2020-05-09 22:26:43 +02:00
|
|
|
|
2021-03-01 06:35:46 +01:00
|
|
|
# The locale can have impact how Firefox does locale-aware sorting,
|
|
|
|
# which we do verify in some tests.
|
2021-04-30 21:57:25 +02:00
|
|
|
os.environ["LC_ALL"] = "C.UTF-8"
|
2021-03-01 06:35:46 +01:00
|
|
|
|
2020-03-16 13:44:15 +01:00
|
|
|
os.environ.pop("http_proxy", "")
|
|
|
|
os.environ.pop("https_proxy", "")
|
|
|
|
|
|
|
|
usage = """test-js-with-puppeteer [options]
|
|
|
|
test-js-with-puppeteer # Run all test files
|
2023-02-22 23:04:11 +01:00
|
|
|
test-js-with-puppeteer navigation.test.ts # Run a single test file
|
|
|
|
test-js-with-puppeteer navi # Run a single test file navigation.test.ts
|
|
|
|
test-js-with-puppeteer login.test.ts compose.test.ts # Run a few test files
|
|
|
|
test-js-with-puppeteer login compose # Run a few test files, login.test.ts and compose.test.ts here"""
|
2020-03-16 13:44:15 +01:00
|
|
|
|
|
|
|
sys.path.insert(0, ZULIP_PATH)
|
|
|
|
|
|
|
|
# check for the venv
|
|
|
|
from tools.lib import sanity_check
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-03-16 13:44:15 +01:00
|
|
|
sanity_check.check_venv(__file__)
|
|
|
|
|
2020-06-17 20:36:00 +02:00
|
|
|
from typing import Iterable, Tuple
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
from tools.lib.test_script import (
|
2021-03-03 05:00:15 +01:00
|
|
|
add_provision_check_override_param,
|
2020-06-11 00:54:34 +02:00
|
|
|
assert_provisioning_status_ok,
|
|
|
|
find_js_test_files,
|
|
|
|
prepare_puppeteer_run,
|
|
|
|
)
|
2020-03-16 13:44:15 +01:00
|
|
|
from tools.lib.test_server import test_server_running
|
|
|
|
|
2021-03-03 05:00:15 +01:00
|
|
|
parser = argparse.ArgumentParser(usage)
|
|
|
|
|
|
|
|
parser.add_argument("--interactive", action="store_true", help="Run tests interactively")
|
|
|
|
add_provision_check_override_param(parser)
|
2021-03-12 12:30:43 +01:00
|
|
|
parser.add_argument("--firefox", action="store_true", help="Run tests with firefox.")
|
2022-03-02 06:30:39 +01:00
|
|
|
parser.add_argument("--loop", nargs="?", type=int, default=1)
|
2021-03-03 05:00:15 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"tests", nargs=argparse.REMAINDER, help="Specific tests to run; by default, runs all tests"
|
|
|
|
)
|
|
|
|
|
|
|
|
options = parser.parse_args()
|
|
|
|
|
2020-03-16 13:44:15 +01:00
|
|
|
|
2022-03-02 06:03:44 +01:00
|
|
|
def run_single_test(test_file: str, test_number: int, total_tests: int) -> int:
|
|
|
|
cmd = [
|
|
|
|
"node",
|
|
|
|
"--unhandled-rejections=strict",
|
|
|
|
os.path.join(ZULIP_PATH, "node_modules/.bin/ts-node"),
|
|
|
|
"--script-mode",
|
|
|
|
"--transpile-only",
|
|
|
|
test_file,
|
|
|
|
]
|
|
|
|
|
|
|
|
test_name = os.path.basename(test_file)
|
2022-04-27 02:10:28 +02:00
|
|
|
cmd_str = shlex.join(cmd)
|
2022-03-02 06:03:44 +01:00
|
|
|
print(
|
|
|
|
f"\n\n===================== ({test_number}/{total_tests}) {test_name} =====================\nRunning {cmd_str}\n\n",
|
|
|
|
flush=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
ret = subprocess.call(cmd)
|
|
|
|
|
|
|
|
# Resetting test environment.
|
|
|
|
reset_zulip_test_database()
|
|
|
|
|
|
|
|
# We are calling to /flush_caches to remove all the server-side caches.
|
|
|
|
response = requests.post("http://zulip.zulipdev.com:9981/flush_caches")
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2022-03-02 06:30:39 +01:00
|
|
|
def run_tests(files: Iterable[str], external_host: str, loop: int = 1) -> None:
|
2023-02-22 23:04:11 +01:00
|
|
|
test_dir = os.path.join(ZULIP_PATH, "web/e2e-tests")
|
2020-04-17 08:51:06 +02:00
|
|
|
test_files = find_js_test_files(test_dir, files)
|
2021-03-31 23:53:37 +02:00
|
|
|
total_tests = len(test_files)
|
2020-03-16 13:44:15 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def run_tests(test_number: int = 0) -> Tuple[int, int]:
|
2020-06-17 20:36:00 +02:00
|
|
|
current_test_num = test_number
|
|
|
|
for test_file in test_files[test_number:]:
|
2022-03-02 06:03:44 +01:00
|
|
|
return_code = run_single_test(test_file, current_test_num + 1, total_tests)
|
|
|
|
if return_code != 0:
|
|
|
|
return return_code, current_test_num
|
2020-06-17 20:36:00 +02:00
|
|
|
current_test_num += 1
|
|
|
|
return 0, -1
|
2020-03-16 13:44:15 +01:00
|
|
|
|
2021-03-18 00:11:54 +01:00
|
|
|
with test_server_running(options.skip_provision_check, external_host):
|
2020-03-16 13:44:15 +01:00
|
|
|
# Important: do this next call inside the `with` block, when Django
|
|
|
|
# will be pointing at the test database.
|
2021-02-12 08:20:45 +01:00
|
|
|
subprocess.check_call("tools/setup/generate-test-credentials")
|
2020-03-16 13:44:15 +01:00
|
|
|
if options.interactive:
|
|
|
|
response = input('Press Enter to run tests, "q" to quit: ')
|
|
|
|
ret = 1
|
2020-06-17 20:36:00 +02:00
|
|
|
failed_test_num = 0
|
2021-02-12 08:20:45 +01:00
|
|
|
while response != "q" and failed_test_num != -1:
|
2020-06-17 20:36:00 +02:00
|
|
|
ret, failed_test_num = run_tests(failed_test_num)
|
2020-03-16 13:44:15 +01:00
|
|
|
if ret != 0:
|
|
|
|
response = input('Tests failed. Press Enter to re-run tests, "q" to quit: ')
|
|
|
|
else:
|
|
|
|
ret = 1
|
2022-03-02 06:30:39 +01:00
|
|
|
for loop_num in range(1, loop + 1):
|
|
|
|
print(f"\n\nRunning tests in loop ({loop_num}/{loop})\n")
|
|
|
|
ret, current_test_num = run_tests()
|
|
|
|
if ret == 0:
|
|
|
|
print(f"{OKGREEN}All tests passed!{ENDC}")
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2020-03-16 13:44:15 +01:00
|
|
|
if ret != 0:
|
2021-05-10 17:50:24 +02:00
|
|
|
failed_test_file_name = os.path.basename(test_files[current_test_num])
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
|
|
|
f"""
|
2021-05-10 17:50:24 +02:00
|
|
|
{FAIL}The Puppeteer frontend tests failed! The failing test was:
|
|
|
|
./tools/test-js-with-puppeteer {"--firefox " if options.firefox else ""}{failed_test_file_name}{ENDC}
|
2020-11-28 19:27:44 +01:00
|
|
|
For help debugging, read:
|
2020-09-05 15:25:49 +02:00
|
|
|
https://zulip.readthedocs.io/en/latest/testing/testing-with-puppeteer.html
|
2021-02-12 08:19:30 +01:00
|
|
|
or report and ask for help in chat.zulip.org""",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2021-03-15 18:39:44 +01:00
|
|
|
if os.environ.get("GITHUB_ACTIONS"):
|
2020-03-26 21:00:05 +01:00
|
|
|
print("", file=sys.stderr)
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
2021-03-15 18:39:44 +01:00
|
|
|
"""
|
|
|
|
See https://docs.github.com/en/rest/reference/actions#artifacts for information
|
|
|
|
on how to view the generated screenshots, which are uploaded as Artifacts.
|
|
|
|
Screenshots are extremely helpful for understanding puppeteer test failures.
|
|
|
|
""",
|
2021-02-12 08:19:30 +01:00
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2020-03-26 21:00:05 +01:00
|
|
|
print("", file=sys.stderr)
|
2020-09-05 15:25:49 +02:00
|
|
|
else:
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
|
|
|
"It's also worthy to see screenshots generated on failure stored under var/puppeteer/*.png"
|
|
|
|
)
|
2020-03-16 13:44:15 +01:00
|
|
|
sys.exit(ret)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-03-16 13:44:15 +01:00
|
|
|
external_host = "zulipdev.com:9981"
|
2021-03-02 20:59:19 +01:00
|
|
|
assert_provisioning_status_ok(options.skip_provision_check)
|
2021-03-12 12:30:43 +01:00
|
|
|
prepare_puppeteer_run(is_firefox=options.firefox)
|
2022-03-02 06:30:39 +01:00
|
|
|
run_tests(options.tests, external_host, options.loop)
|
2020-03-16 13:44:15 +01:00
|
|
|
sys.exit(0)
|