Run Lighthouse against a URL and return the parsed JSON report. Args: url: The URL to audit. report_path: Where to save the JSON report. Returns: The parsed Lighthouse JSON report.
(url: str, report_path: Path)
| 890 | |
| 891 | |
| 892 | def run_lighthouse(url: str, report_path: Path) -> dict[str, Any]: |
| 893 | """Run Lighthouse against a URL and return the parsed JSON report. |
| 894 | |
| 895 | Args: |
| 896 | url: The URL to audit. |
| 897 | report_path: Where to save the JSON report. |
| 898 | |
| 899 | Returns: |
| 900 | The parsed Lighthouse JSON report. |
| 901 | """ |
| 902 | command = [ |
| 903 | *_prepare_lighthouse_command(tuple(get_lighthouse_command())), |
| 904 | url, |
| 905 | "--output=json", |
| 906 | f"--output-path={report_path}", |
| 907 | f"--chrome-path={get_chrome_path()}", |
| 908 | f"--only-categories={','.join(LIGHTHOUSE_CATEGORIES)}", |
| 909 | "--quiet", |
| 910 | "--chrome-flags=--headless=new --no-sandbox --disable-dev-shm-usage", |
| 911 | ] |
| 912 | |
| 913 | try: |
| 914 | subprocess.run( |
| 915 | command, |
| 916 | check=True, |
| 917 | capture_output=True, |
| 918 | text=True, |
| 919 | timeout=LIGHTHOUSE_RUN_TIMEOUT_SECONDS, |
| 920 | ) |
| 921 | except subprocess.CalledProcessError as err: |
| 922 | pytest.fail( |
| 923 | "Lighthouse execution failed.\n" |
| 924 | f"Command: {' '.join(command)}\n" |
| 925 | f"stdout:\n{_format_subprocess_output(err.stdout)}\n" |
| 926 | f"stderr:\n{_format_subprocess_output(err.stderr)}" |
| 927 | ) |
| 928 | except subprocess.TimeoutExpired as err: |
| 929 | pytest.fail( |
| 930 | f"Lighthouse execution timed out after {err.timeout}s.\n" |
| 931 | f"Command: {' '.join(command)}\n" |
| 932 | f"stdout:\n{_format_subprocess_output(err.stdout)}\n" |
| 933 | f"stderr:\n{_format_subprocess_output(err.stderr)}" |
| 934 | ) |
| 935 | return json.loads(report_path.read_text()) |
| 936 | |
| 937 | |
| 938 | def _ensure_lighthouse_app( |
no test coverage detected