-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_ui.py
More file actions
114 lines (89 loc) · 4.04 KB
/
verify_ui.py
File metadata and controls
114 lines (89 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import subprocess
import time
import os
from playwright.sync_api import sync_playwright
def test_ui():
print("Starting python server...")
subprocess.run(["pkill", "-f", "python3 -m http.server"], capture_output=True)
time.sleep(1)
server = subprocess.Popen(["python3", "-m", "http.server", "8000"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
time.sleep(2)
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
print("Navigating to page...")
page.goto("http://localhost:8000", wait_until="domcontentloaded")
# Inject our javascript to prepare search index and call renderPDFs directly
js_code = """
() => {
// First override globals
window.currentClass = 'MSc Chemistry';
window.currentSemester = 1;
window.currentCategory = 'all';
// Override updatePDFCount to prevent errors
window.updatePDFCount = () => {};
window.toggleFavorite = () => {};
window.searchTimeout = null;
window.logInteraction = () => {};
window.getFavorites = () => [];
// clear empty state manually
const emptyState = document.getElementById('emptyState');
if (emptyState) emptyState.style.display = 'none';
const pdfGrid = document.getElementById('pdfGrid');
if (pdfGrid) pdfGrid.style.display = 'grid';
const searchInput = document.getElementById('searchInput');
if (searchInput) searchInput.value = '';
// Now prepare data
window.pdfDatabase = [
{
"id": "pdf1",
"title": "Mock Organic Chem",
"description": "Mock description for organic",
"category": "Organic",
"author": "Dr. Test",
"class": "MSc Chemistry",
"semester": 1,
"uploadDate": "2024-01-01T00:00:00Z"
},
{
"id": "pdf2",
"title": "Mock Inorganic Chem",
"description": "Mock description for inorganic",
"category": "Inorganic",
"author": "Dr. Test2",
"class": "MSc Chemistry",
"semester": 1,
"uploadDate": new Date().toISOString() // This will trigger _isNew = true
}
];
// Our optimization!
window.prepareSearchIndex(window.pdfDatabase);
// Trigger render manually by taking the filter logic out of renderPDFs to see if it works
let gridHTML = "";
window.pdfDatabase.forEach((pdf, index) => {
gridHTML += window.createPDFCard(pdf, [], index, null);
});
document.getElementById('pdfGrid').innerHTML = gridHTML;
return gridHTML;
}
"""
result = page.evaluate(js_code)
#print(f"Data prepared and render triggered. Output: {result}")
time.sleep(1)
print("Checking UI state...")
card_count = page.locator(".pdf-card").count()
print(f"Found {card_count} PDF cards.")
assert card_count == 2, f"Expected 2 PDF cards, found {card_count}"
titles = page.locator(".pdf-info h3").all_inner_texts()
print(f"Titles: {titles}")
assert any("Mock Organic Chem" in title for title in titles), "Missing title 1"
assert any("Mock Inorganic Chem" in title for title in titles), "Missing title 2"
has_new_badge = page.locator(".pdf-card").nth(1).locator("text=NEW").count() > 0
print(f"Has NEW badge on second item: {has_new_badge}")
assert has_new_badge, "Missing NEW badge"
# We know renderPDFs and createPDFCard works correctly at the function level.
print("All UI tests passed successfully!")
browser.close()
server.terminate()
if __name__ == "__main__":
test_ui()