Repository: servo/servo
File: etc/ci/scenario/common_function_for_servo_test.py (Python)

Code before:
            rect.left * dpr,
            rect.top * dpr,
            rect.right * dpr,
            rect.bottom * dpr
        ];
        """,
        element,
    )

    return physical_rect


def element_screenshot(element: WebElement, filename: str):
    if not (filename.lower().endswith(".jpg") or filename.lower().endswith(".jpeg")):
        raise ValueError(f"Invalid file type: {filename}. Expected a .jpg/.jpeg file.")

    try:
        print(f"Scrolling {element}")
        region = element_scroll_into_view_and_rect(element)
        time.sleep(2)
        hdc = HarmonyDeviceConnector()
        hdc.screenshot(filename)

        region = Image.open(filename).crop(region)
        save_path = filename + ".png"
        region.save(save_path)
    except Exception as e:
        print(f"Element Screenshot failed with error: {e}")


def run_test(test_fn, test_name: str, initial_url: str):
    try:
        print("Stopping potential old servo instance ...")
        stop_servo()
        hdc = HarmonyDeviceConnector()
        print(f"Starting new servo instance, loading {initial_url} ...")
        hdc.cmd(f"aa start -a EntryAbility -b org.servo.servo -U {initial_url} --psn --webdriver", timeout=10)
        setup_hdc_forward()
    except Exception as e:
        print(f"Scenario test setup failed with error: {e} (exception: {type(e)})")
        stop_servo()
        sys.exit(1)
    try:
        with HarmonyDevicePerfMode():
            test_fn()
    except Exception as e:
        print(f"Scenario test `{test_name}` failed with error: {e} (exception: {type(e)})")
        hdc.screenshot(f"servo_scenario_{test_name}_error.jpg")
        stop_servo()
        sys.exit(1)


Review comment (suggestion):
this will be `<name>.jpg.png`, right? maybe we could do `name_cropped.png`?
I think hdc screenshot might support png too.

Code after:
        return [
            rect.left * dpr,
            rect.top * dpr,
            rect.right * dpr,
            rect.bottom * dpr
        ];
        """,
        element,
    )

    return physical_rect


def element_screenshot(element: WebElement, filename: str):
    if not (filename.lower().endswith(".jpg") or filename.lower().endswith(".jpeg")):
        raise ValueError(f"Invalid file type: {filename}. Expected a .jpg/.jpeg file.")

    try:
        print(f"Scrolling {element}")
        region = element_scroll_into_view_and_rect(element)
        time.sleep(2)
        hdc = HarmonyDeviceConnector()
        hdc.screenshot(filename)

        region = Image.open(filename).crop(region)
        save_path = filename + ".png"
        region.save(save_path)
    except Exception as e:
        print(f"Element Screenshot failed with error: {e}")


def run_test(test_fn, test_name: str, initial_url: str):
    try:
        print("Stopping potential old servo instance ...")
        stop_servo()
        hdc = HarmonyDeviceConnector()
        print(f"Starting new servo instance, loading {initial_url} ...")
        hdc.cmd(f"aa start -a EntryAbility -b org.servo.servo -U {initial_url} --psn --webdriver", timeout=10)
        setup_hdc_forward()
    except Exception as e:
        print(f"Scenario test setup failed with error: {e} (exception: {type(e)})")
        stop_servo()
        sys.exit(1)
    try:
        with HarmonyDevicePerfMode():
            test_fn()
    except Exception as e:
        print(f"Scenario test `{test_name}` failed with error: {e} (exception: {type(e)})")
        hdc.screenshot(f"servo_scenario_{test_name}_error.jpg")
        stop_servo()
        sys.exit(1)