forked from pywebio/PyWebIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.pin_test.py
114 lines (89 loc) · 4.38 KB
/
18.pin_test.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
import subprocess
import time
from percy import percySnapshot
from selenium.webdriver import Chrome
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
import pywebio
import util
from pywebio import start_server
from pywebio.output import *
from pywebio.pin import *
from pywebio.utils import run_as_function, to_coroutine
PASSED_TEXT = "All-assert-passed"
def target():
options = ['A', 'B', 'C']
put_input('input', label='input')
put_textarea('textarea', label='textarea', rows=3, code=None, maxlength=10, minlength=20, value=None,
placeholder='placeholder', readonly=False, help_text='help_text')
put_textarea('code', label='code', rows=4, code=True, maxlength=10, minlength=20, value=None,
placeholder='placeholder', readonly=False, help_text='help_text')
put_select('select', options=options, label='Input pin widget')
put_select('select_multiple', options=options, label='select-multiple', multiple=True, value=None,
help_text='help_text')
put_checkbox('checkbox', options=options, label='checkbox', inline=False, value=None, help_text='help_text')
put_checkbox('checkbox_inline', options=options, label='checkbox_inline', inline=True, value=None,
help_text='help_text')
put_radio('radio', options=options, label='radio', inline=False, value=None, help_text='help_text')
put_radio('radio_inline', options=options, label='radio_inline', inline=True, value='B', help_text='help_text')
put_actions('actions', buttons=['action_a', 'action_b'], label='actions')
pin_update('input', help_text='This is help text')
pin_update('select_multiple', value=['B', 'C'])
pin.radio = 'B'
assert (yield pin['radio']) == (yield pin.radio) == 'B'
names = ['input', 'textarea', 'code', 'select', 'select_multiple', 'checkbox', 'checkbox_inline', 'radio',
'radio_inline', 'actions']
values = {}
on_change_values = {}
for name in names:
pin_on_change(name, lambda val, name=name: on_change_values.__setitem__(name, val))
while len(names) != len(values):
info = yield pin_wait_change(*names)
values[info['name']] = info['value']
for name in names:
assert (yield pin[name]) == values.get(name) == on_change_values.get(name)
put_text(name, values.get(name), on_change_values.get(name))
put_text(PASSED_TEXT)
def thread_target():
run_as_function(target())
async def coro_target():
await to_coroutine(target())
def test_one_page(browser: Chrome):
browser.find_element_by_css_selector('[name=input]').send_keys("1")
time.sleep(0.5)
browser.find_element_by_css_selector('[name=textarea]').send_keys("2")
time.sleep(0.5)
Select(browser.find_element_by_css_selector('[name=select]')).select_by_visible_text('B')
time.sleep(0.5)
Select(browser.find_element_by_css_selector('[name=select_multiple]')).select_by_visible_text('A')
time.sleep(0.5)
browser.find_element_by_css_selector('[name=checkbox]').click()
time.sleep(0.5)
browser.find_element_by_css_selector('[name=checkbox_inline]').click()
time.sleep(0.5)
browser.find_element_by_css_selector('[name=radio]').click()
time.sleep(0.5)
browser.find_element_by_css_selector('[name=radio_inline]').click()
time.sleep(0.5)
browser.find_element_by_css_selector('button[type=\"submit\"]').click()
time.sleep(0.5)
codeMirror = browser.find_element_by_css_selector(".CodeMirror pre")
action_chains = ActionChains(browser)
action_chains.move_to_element(codeMirror).click(codeMirror).send_keys('3').perform()
def test(server_proc: subprocess.Popen, browser: Chrome):
browser.get('http://localhost:8080/?app=thread_target')
time.sleep(2)
test_one_page(browser)
time.sleep(2)
percySnapshot(browser, name='pin')
assert PASSED_TEXT in browser.find_element_by_id('markdown-body').get_attribute('innerHTML')
browser.get('http://localhost:8080/?app=coro_target')
time.sleep(2)
test_one_page(browser)
time.sleep(1)
assert PASSED_TEXT in browser.find_element_by_id('markdown-body').get_attribute('innerHTML')
def start_test_server():
pywebio.enable_debug()
start_server([thread_target, coro_target], port=8080, host='127.0.0.1', auto_open_webbrowser=False, cdn=False)
if __name__ == '__main__':
util.run_test(start_test_server, test)