code: def assert_snapshot(
      request: FixtureRequest, output_folder: Path
  ) -> Generator[ImageCompareFunction, None, None]:
      """Fixture that compares a screenshot with screenshot from a past run."""
      root_path = Path(os.getcwd()).resolve()
      platform = str(sys.platform)
      # TODO(lukasmasuch): Is there a better way to get the module name?
      module_name = request.module.__name__.split(".")[-1]
      test_function_name = request.node.originalname
  
      snapshot_dir: Path = (
          root_path / "__snapshots__" / platform / module_name
      )
  
      module_snapshot_failures_dir: Path = (
          output_folder
          / "snapshot-tests-failures"
          / platform
          / module_name
      )
      module_snapshot_updates_dir: Path = (
          output_folder / "snapshot-updates" / platform / module_name
      )
  
      snapshot_file_suffix = ""
      # Extract the parameter ids if they exist
      match = re.search(r"\[(.*?)\]", request.node.name)
      if match:
          snapshot_file_suffix = f"[{match.group(1)}]"
  
      snapshot_default_file_name: str = (
          test_function_name + snapshot_file_suffix
      )
  
      test_failure_messages: List[str] = []
  
      def compare(
          element: ElementHandle | Locator | Page,
          *,
          image_threshold: float = 0.002,
          pixel_threshold: float = 0.05,
          name: str | None = None,
          fail_fast: bool = False,
          file_type: Literal["png", "jpg"] = "png",
      ) -> None:
          """Compare a screenshot with screenshot from a past run.
  
          Parameters
          ----------
          element : ElementHandle or Locator
              The element to take a screenshot of.
          image_threshold : float, optional
              The allowed percentage of different pixels in the image.
          pixel_threshold : float, optional
              The allowed percentage of difference for a single pixel to be considered
              different.
          name : str | None, optional
              The name of the screenshot without an extension. If not provided, the name
              of the test function will be used.
          fail_fast : bool, optional
              If True, the comparison will stop at the first pixel mismatch.
          file_type: "png" or "jpg"
              The file type of the screenshot. Defaults to "png".
          """
          nonlocal test_failure_messages
          nonlocal snapshot_default_file_name
          nonlocal module_snapshot_updates_dir
          nonlocal module_snapshot_failures_dir
          nonlocal snapshot_file_suffix
  
          if file_type == "jpg":
              file_extension = ".jpg"
              img_bytes = element.screenshot(
                  type="jpeg", quality=90, animations="disabled"
              )
  
          else:
              file_extension = ".png"
              img_bytes = element.screenshot(
                  type="png", animations="disabled"
              )
  
          snapshot_file_name: str = snapshot_default_file_name
          if name:
              snapshot_file_name = name + snapshot_file_suffix
  
          snapshot_file_path: Path = (
              snapshot_dir / f"{snapshot_file_name}{file_extension}"
          )
  
          snapshot_updates_file_path: Path = (
              module_snapshot_updates_dir
              / f"{snapshot_file_name}{file_extension}"
          )
  
          snapshot_file_path.parent.mkdir(parents=True, exist_ok=True)
  
          test_failures_dir = (
              module_snapshot_failures_dir / snapshot_file_name
          )
          if test_failures_dir.exists():
              # Remove the past runs failure dir for this specific screenshot
              shutil.rmtree(test_failures_dir)
  
          if not snapshot_file_path.exists():
              snapshot_file_path.write_bytes(img_bytes)
              # Update this in updates folder:
              snapshot_updates_file_path.parent.mkdir(
                  parents=True, exist_ok=True
              )
              snapshot_updates_file_path.write_bytes(img_bytes)
              # For missing snapshots, we don't want to directly fail in order to generate
              # all missing snapshots in one run.
              test_failure_messages.append(
                  f"Missing snapshot for {snapshot_file_name}"
              )
              return
  
          from pixelmatch.contrib.PIL import pixelmatch
  
          # Compare the new screenshot with the screenshot from past runs:
          img_a = Image.open(BytesIO(img_bytes))
          img_b = Image.open(snapshot_file_path)
          img_diff = Image.new("RGBA", img_a.size)
          try:
              mismatch = pixelmatch(
                  img_a,
                  img_b,
                  img_diff,
                  threshold=pixel_threshold,
                  fail_fast=fail_fast,
                  alpha=0,
              )
          except ValueError as ex:
              # ValueError is thrown when the images have different sizes
              # Update this in updates folder:
              snapshot_updates_file_path.parent.mkdir(
                  parents=True, exist_ok=True
              )
              snapshot_updates_file_path.write_bytes(img_bytes)
              pytest.fail(
                  f"Snapshot matching for {snapshot_file_name} failed: {ex}"
              )
          max_diff_pixels = int(
              image_threshold * img_a.size[0] * img_a.size[1]
          )
  
          if mismatch < max_diff_pixels:
              return
  
          # Update this in updates folder:
          snapshot_updates_file_path.parent.mkdir(
              parents=True, exist_ok=True
          )
          snapshot_updates_file_path.write_bytes(img_bytes)
  
          # Create new failures folder for this test:
          test_failures_dir.mkdir(parents=True, exist_ok=True)
          img_diff.save(
              f"{test_failures_dir}/diff_{snapshot_file_name}{file_extension}"
          )
          img_a.save(
              f"{test_failures_dir}/actual_{snapshot_file_name}{file_extension}"
          )
          img_b.save(
              f"{test_failures_dir}/expected_{snapshot_file_name}{file_extension}"
          )
  
          pytest.fail(
              f"Snapshot mismatch for {snapshot_file_name} ({mismatch} pixels difference)"
          )
  
      yield compare
  
      if test_failure_messages:
          pytest.fail(
              "Missing snapshots: \n" + "\n".join(test_failure_messages)
          )
  
imports: from __future__ import annotations
  import hashlib
  import os
  import re
  import shlex
  import shutil
  import socket
  import subprocess
  import sys
  import time
  from io import BytesIO
  from pathlib import Path
  from random import randint
  from tempfile import TemporaryFile
  from types import ModuleType
  from typing import Any, Dict, Generator, List, Literal, Protocol, Tuple
  from urllib import parse
  import pytest
  import requests
  from PIL import Image
  from playwright.sync_api import ElementHandle, Locator, Page
  from pytest import FixtureRequest
label_desc: Use image node_id 19 for calling a global function with example usage: assert_snapshot(request, output_folder) without return types