"""Utilities for building and running the Appium automation Docker container."""
from __future__ import annotations
import logging
import os
import subprocess
from typing import Optional
logger = logging.getLogger(__name__)
def build_and_run_docker(
output_dir: str = "output",
image_name: str = "appium-ui-dump",
dockerfile_dir: Optional[str] = None,
) -> bool:
"""
Build the Docker image and run the container, capturing the UI dump.
Returns ``True`` on success, otherwise ``False``.
"""
output_path = os.path.abspath(output_dir)
os.makedirs(output_path, exist_ok=True)
build_context = dockerfile_dir or "."
logger.info(
"Building Docker image '%s' from context '%s'", image_name, build_context
)
build_cmd = ["docker", "build", "-t", image_name, build_context]
try:
subprocess.run(build_cmd, check=True)
except subprocess.CalledProcessError as exc:
logger.error("Error building Docker image: %s", exc, exc_info=True)
return False
logger.info("Running Docker container '%s'", image_name)
run_cmd = [
"docker",
"run",
"--rm",
"-v",
f"{output_path}:/output",
image_name,
]
try:
subprocess.run(run_cmd, check=True)
except subprocess.CalledProcessError as exc:
logger.error("Error running Docker container: %s", exc, exc_info=True)
return False
xml_path = os.path.join(output_path, "ui-automator-dump.xml")
if not os.path.exists(xml_path):
logger.warning("UI dump XML file not found at %s", xml_path)
return False
logger.info("UI Automator dump saved to %s", xml_path)
return True
__all__ = ["build_and_run_docker"]