forked from zauberzeug/nicegui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_javascript.py
91 lines (71 loc) · 2.68 KB
/
test_javascript.py
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
import pytest
from nicegui import Client, ui
from nicegui.events import ValueChangeEventArguments
from .screen import Screen
def test_run_javascript_on_button_press(screen: Screen):
async def set_title() -> None:
await ui.run_javascript('document.title = "A New Title"')
ui.button('change title', on_click=set_title)
screen.open('/')
assert screen.selenium.title == 'NiceGUI'
screen.click('change title')
screen.wait(0.5)
screen.should_contain('A New Title')
def test_run_javascript_on_value_change(screen: Screen):
@ui.page('/')
async def page(client: Client):
async def set_title(e: ValueChangeEventArguments) -> None:
await ui.run_javascript(f'document.title = "Page {e.value}"')
ui.radio(['A', 'B'], on_change=set_title)
await client.connected()
await ui.run_javascript('document.title = "Initial Title"')
screen.open('/')
screen.wait(0.5)
screen.should_contain('Initial Title')
screen.click('A')
screen.wait(0.5)
screen.should_contain('Page A')
screen.click('B')
screen.wait(0.5)
screen.should_contain('Page B')
def test_run_javascript_before_client_connected(screen: Screen):
@ui.page('/')
async def page():
ui.label('before js')
with pytest.raises(RuntimeError):
await ui.run_javascript('document.title = "A New Title"')
ui.label('after js')
screen.open('/')
assert screen.selenium.title == 'NiceGUI'
screen.should_contain('before js')
screen.should_contain('after js')
def test_response_from_javascript(screen: Screen):
async def compute() -> None:
response = await ui.run_javascript('1 + 41')
ui.label(response)
ui.button('compute', on_click=compute)
screen.open('/')
screen.click('compute')
screen.should_contain('42')
def test_async_javascript(screen: Screen):
async def run():
result = await ui.run_javascript('await new Promise(r => setTimeout(r, 100)); return 42')
ui.label(result)
ui.button('run', on_click=run)
screen.open('/')
screen.click('run')
screen.should_contain('42')
def test_simultaneous_async_javascript(screen: Screen):
async def runA():
result = await ui.run_javascript('await new Promise(r => setTimeout(r, 500)); return 1')
ui.label(f'A: {result}')
async def runB():
result = await ui.run_javascript('await new Promise(r => setTimeout(r, 250)); return 2')
ui.label(f'B: {result}')
ui.button('runA', on_click=runA)
ui.button('runB', on_click=runB)
screen.open('/')
screen.click('runA')
screen.click('runB')
screen.should_contain('A: 1')
screen.should_contain('B: 2')