Raymond Weitekamp commited on
Commit
ffec248
·
1 Parent(s): fcd4d8a

feat: add server test script for HF Space - Add test_server.sh to run tests against deployed Space - Test logged-in state and UI elements on actual Space - Use saved auth state for authentication

Browse files
Files changed (1) hide show
  1. test_server.sh +92 -0
test_server.sh ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Exit on error
4
+ set -e
5
+
6
+ # Check if auth_state.json exists
7
+ if [ ! -f "auth_state.json" ]; then
8
+ echo "Error: auth_state.json not found. Please run save_hf_storage_state.py first to save your Hugging Face login state."
9
+ exit 1
10
+ fi
11
+
12
+ # Activate virtual environment if it exists, create if it doesn't
13
+ if [ ! -d "venv" ]; then
14
+ echo "Creating virtual environment..."
15
+ python -m venv venv
16
+ fi
17
+
18
+ echo "Activating virtual environment..."
19
+ source venv/bin/activate
20
+
21
+ # Install dependencies if needed
22
+ echo "Installing dependencies..."
23
+ if ! command -v uv &> /dev/null; then
24
+ echo "Installing uv package installer..."
25
+ curl -LsSf https://astral.sh/uv/install.sh | sh
26
+ fi
27
+ uv pip install -r requirements.txt
28
+
29
+ # Install Playwright browsers if needed
30
+ echo "Installing Playwright browsers..."
31
+ playwright install chromium
32
+
33
+ # Create a new test file specifically for server tests
34
+ cat > test_server.py << 'EOL'
35
+ import os
36
+ import pytest
37
+ import pytest_asyncio
38
+ from playwright.async_api import async_playwright, expect
39
+ import asyncio
40
+
41
+ @pytest_asyncio.fixture(scope="session")
42
+ def event_loop():
43
+ """Create an instance of the default event loop for each test case."""
44
+ policy = asyncio.get_event_loop_policy()
45
+ loop = policy.new_event_loop()
46
+ yield loop
47
+ loop.close()
48
+
49
+ @pytest.mark.asyncio
50
+ async def test_space_loads_logged_in():
51
+ """Test that we can access the Space with our saved authentication state"""
52
+ auth_state_path = "auth_state.json"
53
+ assert os.path.exists(auth_state_path), f"Authentication state file not found at {auth_state_path}"
54
+
55
+ async with async_playwright() as playwright:
56
+ # Launch browser with stored authentication state
57
+ browser = await playwright.chromium.launch(headless=False)
58
+ context = await browser.new_context(storage_state=auth_state_path)
59
+ page = await context.new_page()
60
+
61
+ try:
62
+ # Navigate to the Space
63
+ await page.goto("https://huggingface.co/spaces/rawwerks/handwriting-ocr")
64
+ await page.wait_for_load_state("networkidle")
65
+
66
+ # Check if the main heading is visible
67
+ await expect(page.locator("h2", has_text="Crowdsourcing Handwriting OCR Dataset")).to_be_visible()
68
+
69
+ # Check for logged-in state
70
+ await expect(page.locator("text=Logged in as:")).to_be_visible()
71
+
72
+ # Check that the interface elements are visible
73
+ await expect(page.get_by_label("Text to Handwrite")).to_be_visible()
74
+ await expect(page.get_by_label("Upload Handwritten Image")).to_be_visible()
75
+ await expect(page.get_by_role("button", name="Submit")).to_be_visible()
76
+ await expect(page.get_by_role("button", name="Skip")).to_be_visible()
77
+
78
+ finally:
79
+ await context.close()
80
+ await browser.close()
81
+ EOL
82
+
83
+ # Run the server tests
84
+ echo "Running server tests..."
85
+ python -m pytest test_server.py -v
86
+
87
+ # Cleanup
88
+ echo "Cleaning up..."
89
+ rm test_server.py
90
+
91
+ # Deactivate virtual environment
92
+ deactivate