test_chart.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from selenium.webdriver.common.by import By
  2. from nicegui import ui
  3. from .screen import Screen
  4. def get_series_0(selenium):
  5. return selenium.find_elements(By.CSS_SELECTOR, '.highcharts-series-0 .highcharts-point')
  6. def test_change_chart_series(screen: Screen):
  7. chart = ui.chart({
  8. 'title': False,
  9. 'chart': {'type': 'bar'},
  10. 'xAxis': {'categories': ['A', 'B']},
  11. 'series': [
  12. {'name': 'Alpha', 'data': [0.1, 0.2]},
  13. {'name': 'Beta', 'data': [0.3, 0.4]},
  14. ],
  15. }).classes('w-full h-64')
  16. def update():
  17. chart.options['series'][0]['data'][:] = [1, 1]
  18. chart.update()
  19. ui.button('Update', on_click=update)
  20. screen.open('/')
  21. screen.wait(0.5)
  22. before = [bar.size['width'] for bar in get_series_0(screen.selenium)]
  23. screen.click('Update')
  24. screen.wait(0.5)
  25. after = [bar.size['width'] for bar in get_series_0(screen.selenium)]
  26. assert before[0] < after[0]
  27. assert before[1] < after[1]
  28. def test_adding_chart_series(screen: Screen):
  29. chart = ui.chart({
  30. 'title': False,
  31. 'chart': {'type': 'bar'},
  32. }).classes('w-full h-64')
  33. def update():
  34. chart.options['xAxis'] = {'categories': ['A', 'B']}
  35. chart.options['series'] = [
  36. {'name': 'Alpha', 'data': [0.1, 0.2]},
  37. {'name': 'Beta', 'data': [0.3, 0.4]},
  38. ]
  39. chart.update()
  40. ui.button('Update', on_click=update)
  41. screen.open('/')
  42. screen.click('Update')
  43. screen.wait(.5)
  44. assert len(screen.selenium.find_elements(By.CSS_SELECTOR, '.highcharts-point')) == 6
  45. def test_removing_chart_series(screen: Screen):
  46. chart = ui.chart({
  47. 'title': False,
  48. 'chart': {'type': 'bar'},
  49. 'series': [
  50. {'name': 'Alpha', 'data': [0.1, 0.2]},
  51. {'name': 'Beta', 'data': [0.3, 0.4]},
  52. ],
  53. }).classes('w-full h-64')
  54. def update():
  55. chart.options['xAxis'] = {'categories': ['A', 'B']}
  56. chart.options['series'] = [
  57. {'name': 'Alpha', 'data': [0.1, 0.2]},
  58. ]
  59. chart.update()
  60. ui.button('Update', on_click=update)
  61. screen.open('/')
  62. screen.click('Update')
  63. screen.wait(.5)
  64. assert len(screen.selenium.find_elements(By.CSS_SELECTOR, '.highcharts-point')) == 3