forked from pywebio/PyWebIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.misc.py
157 lines (111 loc) · 3.8 KB
/
13.misc.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import asyncio
import subprocess
from functools import partial
from percy import percySnapshot
from selenium.webdriver import Chrome
import pywebio
import template
import util
from pywebio import start_server
from pywebio.input import *
from pywebio.output import *
from pywebio.session import *
from pywebio.utils import *
def target():
# test session data
g = data()
assert g.none is None
g.one = 1
g.one += 1
assert g.one == 2
# test pywebio.utils
async def corofunc(**kwargs):
pass
def genfunc(**kwargs):
yield
corofunc = partial(corofunc, a=1)
genfunc = partial(genfunc, a=1)
assert isgeneratorfunction(genfunc)
assert iscoroutinefunction(corofunc)
assert get_function_name(corofunc) == 'corofunc'
get_free_port()
try:
yield put_buttons([{'label': 'must not be shown'}], onclick=[lambda: None])
except Exception:
pass
put_table([
['Idx', 'Actions'],
['1', put_buttons(['edit', 'delete'], onclick=lambda _: None)],
])
popup('title', 'text content')
@popup('Popup title')
def show_popup():
put_html('<h3>Popup Content</h3>')
put_text('html: <br/>')
with popup('Popup title') as s:
put_html('<h3>Popup Content</h3>')
clear(s)
put_buttons(['clear()'], onclick=lambda _: clear(s))
popup('title2', 'text content')
close_popup()
with use_scope() as name:
put_text('no show')
remove(name)
with use_scope('test') as name:
put_text('current scope name:%s' % name)
with use_scope('test', clear=True):
put_text('clear previous scope content')
@use_scope('test')
def scoped_func(text):
put_text(text)
scoped_func('text1 from `scoped_func`')
scoped_func('text2 from `scoped_func`')
try:
put_column([put_text('A'), 'error'])
except Exception:
pass
toast('Awesome PyWebIO!!', duration=0)
def show_msg():
put_text("ToastClicked")
toast('You have new messages', duration=0, onclick=show_msg)
run_js("$('.toastify').eq(0).click()")
assert get_scope() == 'ROOT'
with use_scope('go_app'):
put_buttons(['Go thread App'], [lambda: go_app('thread', new_window=False)])
# test unhandled error
with use_scope('error'):
put_buttons(['Raise error'], [lambda: 1 / 0])
put_text('\n' * 40)
yield input_group('test input popup', [
input('username', name='user'),
actions('', ['Login', 'Register', 'Forget'], name='action')
])
async def corobased():
await wait_host_port(port=8080, host='127.0.0.1')
async def bg_task():
while 1:
await asyncio.sleep(1)
run_async(bg_task())
await to_coroutine(target())
def threadbased():
run_as_function(target())
def test(server_proc: subprocess.Popen, browser: Chrome):
time.sleep(2)
percySnapshot(browser=browser, name='misc output')
coro_out = template.save_output(browser)[-1]
# browser.get('http://localhost:8080/?app=thread')
browser.execute_script("arguments[0].click();",
browser.find_element_by_css_selector('#pywebio-scope-go_app button'))
time.sleep(2)
thread_out = template.save_output(browser)[-1]
assert "ToastClicked" in coro_out
assert coro_out == thread_out
browser.execute_script("arguments[0].click();",
browser.find_element_by_css_selector('#pywebio-scope-error button'))
browser.execute_script("$('button[type=submit]').click();")
time.sleep(2)
def start_test_server():
pywebio.enable_debug()
start_server({'coro': corobased, 'thread': threadbased}, port=8080, host='127.0.0.1', debug=True)
if __name__ == '__main__':
util.run_test(start_test_server, test, address='http://localhost:8080/?app=thread')