13.misc.py 3.8 KB

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