13.misc.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.output import *
  11. from pywebio.session import *
  12. from pywebio.utils import *
  13. def target():
  14. # test session data
  15. g = data()
  16. assert g.none is None
  17. g.one = 1
  18. g.one += 1
  19. assert g.one == 2
  20. # test pywebio.utils
  21. async def corofunc(**kwargs):
  22. pass
  23. def genfunc(**kwargs):
  24. yield
  25. corofunc = partial(corofunc, a=1)
  26. genfunc = partial(genfunc, a=1)
  27. assert isgeneratorfunction(genfunc)
  28. assert iscoroutinefunction(corofunc)
  29. assert get_function_name(corofunc) == 'corofunc'
  30. get_free_port()
  31. try:
  32. yield put_buttons([{'label': 'must not be shown'}], onclick=[lambda: None])
  33. except Exception:
  34. pass
  35. # test deprecated api
  36. put_table([
  37. ['Idx', 'Actions'],
  38. ['1', table_cell_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("Toast clicked")
  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. yield hold()
  76. async def corobased():
  77. await wait_host_port(port=8080, host='127.0.0.1')
  78. await to_coroutine(target())
  79. def threadbased():
  80. run_as_function(target())
  81. def test(server_proc: subprocess.Popen, browser: Chrome):
  82. time.sleep(2)
  83. percySnapshot(browser=browser, name='misc output')
  84. coro_out = template.save_output(browser)[-1]
  85. # browser.get('http://localhost:8080/?app=thread')
  86. browser.execute_script("arguments[0].click();", browser.find_element_by_css_selector('#pywebio-scope-go_app button'))
  87. time.sleep(2)
  88. thread_out = template.save_output(browser)[-1]
  89. assert coro_out == thread_out
  90. def start_test_server():
  91. pywebio.enable_debug()
  92. start_server({'coro': corobased, 'thread': threadbased}, port=8080, host='127.0.0.1', debug=True)
  93. if __name__ == '__main__':
  94. util.run_test(start_test_server, test, address='http://localhost:8080/?app=coro')