import pytest import os from playwright.sync_api import expect from PIL import Image import numpy as np import tempfile # Constants GRADIO_PORT = 7862 GRADIO_URL = f"http://localhost:{GRADIO_PORT}" @pytest.fixture(scope="module") def test_image(): # Create a temporary test image test_img = Image.fromarray(np.zeros((100, 100, 3), dtype=np.uint8)) temp_dir = tempfile.mkdtemp() test_img_path = os.path.join(temp_dir, "test_image.png") test_img.save(test_img_path) yield test_img_path # Cleanup os.remove(test_img_path) os.rmdir(temp_dir) def test_page_loads(page): page.goto(GRADIO_URL) page.wait_for_load_state("networkidle") # Check if title is present with exact text expect(page.locator("h2", has_text="Crowdsourcing Handwriting OCR Dataset")).to_be_visible() # Check if main interface elements are present expect(page.get_by_label("Text to Handwrite")).to_be_visible() expect(page.locator('input[type="file"]')).to_be_attached() expect(page.get_by_role("button", name="Submit")).to_be_visible() expect(page.get_by_role("button", name="Skip")).to_be_visible() def test_skip_functionality(page): page.goto(GRADIO_URL) page.wait_for_load_state("networkidle") # Get initial text text_box = page.get_by_label("Text to Handwrite") initial_text = text_box.input_value() # Click skip button page.get_by_role("button", name="Skip").click() page.wait_for_timeout(2000) # Wait for response # Get new text and verify it changed new_text = text_box.input_value() assert initial_text != new_text def test_upload_image(page, test_image): page.goto(GRADIO_URL) page.wait_for_load_state("networkidle") # Get initial text text_box = page.get_by_label("Text to Handwrite") initial_text = text_box.input_value() # Upload image - file input is hidden, but we can still set its value page.locator('input[type="file"]').set_input_files(test_image) page.wait_for_timeout(2000) # Wait for upload # Click submit to complete the upload page.get_by_role("button", name="Submit").click() page.wait_for_timeout(2000) # Wait for response # Verify text changed after submission new_text = text_box.input_value() assert initial_text != new_text