File size: 3,130 Bytes
afd13cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ba5932
 
 
 
afd13cb
 
8fb90d0
eeed827
afd13cb
 
 
 
 
 
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
"""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")


@pytest.fixture(scope="module")
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()