Download tests/test_guide_browser.py from wzender/mathly: direct link, hf CLI and curl.
- Browser
- Download file 3.13 kB
-
https://huggingface.co/spaces/wzender/mathly/resolve/main/tests/test_guide_browser.py
- Command line
-
hf download hf://spaces/wzender/mathly/tests/test_guide_browser.py
-
curl -L -o test_guide_browser.py https://huggingface.co/spaces/wzender/mathly/resolve/main/tests/test_guide_browser.py
3.13 kB
| """Browser checks of the user guide (static/guide.js): the "?" button opens it, with a tab for students and one for teachers. | |
| Optional, like the other browser tests: pip install playwright && playwright install chromium.""" | |
| import socket | |
| import threading | |
| import time | |
| import pytest | |
| import uvicorn | |
| from app.main import create_app | |
| playwright = pytest.importorskip("playwright.sync_api") | |
| def server(): | |
| app = create_app() | |
| with socket.socket() as sock: | |
| sock.bind(("127.0.0.1", 0)) | |
| srv = uvicorn.Server(uvicorn.Config(app, log_level="error")) | |
| thread = threading.Thread(target=srv.run, kwargs={"sockets": [sock]}, daemon=True) | |
| thread.start() | |
| try: | |
| deadline = time.monotonic() + 5 | |
| while not srv.started: | |
| assert thread.is_alive() and time.monotonic() < deadline | |
| time.sleep(.01) | |
| yield f"http://127.0.0.1:{sock.getsockname()[1]}" | |
| finally: | |
| srv.should_exit = True | |
| thread.join(timeout=5) | |
| def test_guide_opens_with_student_and_teacher_tabs(server): | |
| with playwright.sync_playwright() as p: | |
| browser = p.chromium.launch() | |
| try: | |
| ctx = browser.new_context(viewport={"width": 390, "height": 844}) | |
| ctx.route("https://cdn.jsdelivr.net/**", lambda route: route.abort()) | |
| ctx.route("https://fonts.googleapis.com/**", lambda route: route.abort()) | |
| page = ctx.new_page() | |
| errors = [] | |
| page.on("pageerror", lambda e: errors.append(str(e))) | |
| page.goto(server) | |
| assert page.locator("#boot [data-guide-open]").count() == 1 # on the sign-in screen | |
| page.locator("#mathView [data-guide-open]").first.click() # and in the app's header | |
| guide = page.get_by_role("dialog", name="讗讬讱 诪砖转诪砖讬诐 讘讗驻诇讬拽爪讬讛") | |
| playwright.expect(guide).to_be_visible() | |
| playwright.expect(page.locator("#mgS")).to_contain_text("讘讜讞谉 诪住讻诐") | |
| playwright.expect(page.locator("#mgS")).to_contain_text("讗驻砖专 诇注砖讜转 讻诪讛 驻注诪讬诐 砖专讜爪讬诐, 讜讛爪讬讜谉 讛讻讬 讟讜讘 讛讜讗 砖谞砖讗专") # 讘讜讞谉 | |
| playwright.expect(page.locator("#mgS")).to_contain_text("诪讙讬砖讬诐 驻注诐 讗讞转") # 诪讘讞谉 | |
| playwright.expect(page.locator("#mgS")).to_contain_text("讘讬转 讜诪住诇讜诇") # the two tabs | |
| playwright.expect(page.locator("#mgS")).to_contain_text("诪讘讞谉 转专讙讜诇 讻诪讜 讘诪讘讞谉") # the quizzes before an exam | |
| playwright.expect(page.locator("#mgT")).to_be_hidden() | |
| page.get_by_role("tab", name="诇诪讜专讬诐").click() | |
| playwright.expect(page.locator("#mgT")).to_contain_text("讛讻讬转讜转 砖诇讬") | |
| playwright.expect(page.locator("#mgT")).to_contain_text("转讜专 AI") | |
| playwright.expect(page.locator("#mgS")).to_be_hidden() | |
| page.keyboard.press("Escape") | |
| playwright.expect(guide).to_be_hidden() | |
| assert errors == [] | |
| finally: | |
| browser.close() | |