13.misc.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. set_auto_scroll_bottom(False)
  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. # test pywebio.output
  33. set_output_fixed_height(False)
  34. try:
  35. yield put_buttons([{'label': 'must not be shown'}], onclick=[lambda: None])
  36. except Exception:
  37. pass
  38. put_table([
  39. ['Idx', 'Actions'],
  40. ['1', table_cell_buttons(['edit', 'delete'], onclick=lambda _: None)],
  41. ])
  42. popup('title', 'html content')
  43. popup('title2', 'html content')
  44. close_popup()
  45. with use_scope() as name:
  46. put_text('no show')
  47. remove(name)
  48. with use_scope('test') as name:
  49. put_text('current scope name:%s' % name)
  50. with use_scope('test', clear=True):
  51. put_text('clear previous scope content')
  52. @use_scope('test')
  53. def scoped_func(text):
  54. put_text(text)
  55. scoped_func('text1 from `scoped_func`')
  56. scoped_func('text2 from `scoped_func`')
  57. try:
  58. put_column([put_text('A'), 'error'])
  59. except Exception:
  60. pass
  61. yield hold()
  62. async def corobased():
  63. await wait_host_port(port=8080, host='127.0.0.1')
  64. await to_coroutine(target())
  65. def threadbased():
  66. run_as_function(target())
  67. def test(server_proc: subprocess.Popen, browser: Chrome):
  68. time.sleep(2)
  69. percySnapshot(browser=browser, name='misc output')
  70. coro_out = template.save_output(browser)[-1]
  71. browser.get('http://localhost:8080/?app=thread')
  72. time.sleep(2)
  73. thread_out = template.save_output(browser)[-1]
  74. assert coro_out == thread_out
  75. def start_test_server():
  76. pywebio.enable_debug()
  77. start_server({'coro': corobased, 'thread': threadbased}, port=8080, host='127.0.0.1', debug=True)
  78. if __name__ == '__main__':
  79. util.run_test(start_test_server, test, address='http://localhost:8080/?app=coro')