13.misc.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import asyncio
  2. import subprocess
  3. from functools import partial
  4. from percy import percySnapshot
  5. from selenium.webdriver import Chrome
  6. import pywebio
  7. import template
  8. import util
  9. from pywebio import start_server
  10. from pywebio.input import *
  11. from pywebio.output import *
  12. from pywebio.session import *
  13. from pywebio.utils import *
  14. from pywebio.platform import seo
  15. def target():
  16. # test session data
  17. g = data()
  18. assert g.none is None
  19. g.one = 1
  20. g.one += 1
  21. assert g.one == 2
  22. local.name = "Wang"
  23. local.age = 22
  24. assert len(local) == 3
  25. assert local['age'] is local.age
  26. assert local.foo is None
  27. local[10] = "10"
  28. del local['name']
  29. del local.one
  30. for key in local:
  31. print(key)
  32. assert 'bar' not in local
  33. assert 'age' in local
  34. assert local._dict == {'age': 22, 10: '10'}
  35. print(local)
  36. # test pywebio.utils
  37. async def corofunc(**kwargs):
  38. pass
  39. def genfunc(**kwargs):
  40. yield
  41. corofunc = partial(corofunc, a=1)
  42. genfunc = partial(genfunc, a=1)
  43. assert isgeneratorfunction(genfunc)
  44. assert iscoroutinefunction(corofunc)
  45. assert get_function_name(corofunc) == 'corofunc'
  46. get_free_port()
  47. try:
  48. yield put_buttons([{'label': 'must not be shown'}], onclick=[lambda: None])
  49. except Exception:
  50. pass
  51. put_table([
  52. ['Idx', 'Actions'],
  53. ['1', put_buttons(['edit', 'delete'], onclick=lambda _: None)],
  54. ])
  55. popup('title', 'text content')
  56. @popup('Popup title')
  57. def show_popup():
  58. put_html('<h3>Popup Content</h3>')
  59. put_text('html: <br/>')
  60. with popup('Popup title') as s:
  61. put_html('<h3>Popup Content</h3>')
  62. clear(s)
  63. put_buttons(['clear()'], onclick=lambda _: clear(s))
  64. popup('title2', 'text content')
  65. close_popup()
  66. with use_scope() as name:
  67. put_text('no show')
  68. remove(name)
  69. with use_scope('test') as name:
  70. put_text('current scope name:%s' % name)
  71. with use_scope('test', clear=True):
  72. put_text('clear previous scope content')
  73. @use_scope('test')
  74. def scoped_func(text):
  75. put_text(text)
  76. scoped_func('text1 from `scoped_func`')
  77. scoped_func('text2 from `scoped_func`')
  78. try:
  79. put_column([put_text('A'), 'error'])
  80. except Exception:
  81. pass
  82. toast('Awesome PyWebIO!!', duration=0)
  83. def show_msg():
  84. put_text("ToastClicked")
  85. toast('You have new messages', duration=0, onclick=show_msg)
  86. run_js("$('.toastify').eq(0).click()")
  87. assert get_scope() == 'ROOT'
  88. with use_scope('go_app'):
  89. put_buttons(['Go thread App'], [lambda: go_app('threadbased', new_window=False)])
  90. # test unhandled error
  91. with use_scope('error'):
  92. put_buttons(['Raise error'], [lambda: 1 / 0])
  93. put_text('\n' * 40)
  94. yield input_group('test input popup', [
  95. input('username', name='user'),
  96. actions('', ['Login', 'Register', 'Forget'], name='action')
  97. ])
  98. @seo("corobased-session", 'This is corobased-session test')
  99. async def corobased():
  100. await wait_host_port(port=8080, host='127.0.0.1')
  101. async def bg_task():
  102. while 1:
  103. await asyncio.sleep(1)
  104. run_async(bg_task())
  105. await to_coroutine(target())
  106. def threadbased():
  107. """threadbased-session
  108. This is threadbased-session test
  109. """
  110. port = get_free_port()
  111. print('free port', port)
  112. run_as_function(target())
  113. def test(server_proc: subprocess.Popen, browser: Chrome):
  114. time.sleep(2)
  115. percySnapshot(browser=browser, name='misc output')
  116. coro_out = template.save_output(browser)[-1]
  117. # browser.get('http://localhost:8080/?app=thread')
  118. browser.execute_script("arguments[0].click();",
  119. browser.find_element_by_css_selector('#pywebio-scope-go_app button'))
  120. time.sleep(2)
  121. thread_out = template.save_output(browser)[-1]
  122. assert "ToastClicked" in coro_out
  123. assert coro_out == thread_out
  124. browser.execute_script("arguments[0].click();",
  125. browser.find_element_by_css_selector('#pywebio-scope-error button'))
  126. browser.execute_script("$('button[type=submit]').click();")
  127. time.sleep(2)
  128. browser.get('http://localhost:8080/')
  129. time.sleep(2)
  130. def start_test_server():
  131. pywebio.enable_debug()
  132. start_server([corobased, partial(threadbased)], port=8080, host='127.0.0.1', debug=True)
  133. if __name__ == '__main__':
  134. util.run_test(start_test_server, test, address='http://localhost:8080/?app=corobased')