test_table.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from selenium.webdriver.common.by import By
  2. from nicegui import ui
  3. from .screen import Screen
  4. columns = [
  5. {'name': 'name', 'label': 'Name', 'field': 'name', 'required': True},
  6. {'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
  7. ]
  8. rows = [
  9. {'id': 0, 'name': 'Alice', 'age': 18},
  10. {'id': 1, 'name': 'Bob', 'age': 21},
  11. {'id': 2, 'name': 'Lionel', 'age': 19},
  12. ]
  13. def test_table(screen: Screen):
  14. ui.table(title='My Team', columns=columns, rows=rows)
  15. screen.open('/')
  16. screen.should_contain('My Team')
  17. screen.should_contain('Name')
  18. screen.should_contain('Alice')
  19. screen.should_contain('Bob')
  20. screen.should_contain('Lionel')
  21. def test_pagination(screen: Screen):
  22. ui.table(columns=columns, rows=rows, pagination=2)
  23. screen.open('/')
  24. screen.should_contain('Alice')
  25. screen.should_contain('Bob')
  26. screen.should_not_contain('Lionel')
  27. screen.should_contain('1-2 of 3')
  28. def test_filter(screen: Screen):
  29. table = ui.table(columns=columns, rows=rows)
  30. ui.input('Search by name').bind_value(table, 'filter')
  31. screen.open('/')
  32. screen.should_contain('Alice')
  33. screen.should_contain('Bob')
  34. screen.should_contain('Lionel')
  35. element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Search by name"]')
  36. element.send_keys('e')
  37. screen.should_contain('Alice')
  38. screen.should_not_contain('Bob')
  39. screen.should_contain('Lionel')
  40. def test_add_remove(screen: Screen):
  41. table = ui.table(columns=columns, rows=rows)
  42. ui.button('Add', on_click=lambda: table.add_rows({'id': 3, 'name': 'Carol', 'age': 32}))
  43. ui.button('Remove', on_click=lambda: table.remove_rows(table.rows[0]))
  44. screen.open('/')
  45. screen.click('Add')
  46. screen.should_contain('Carol')
  47. screen.click('Remove')
  48. screen.should_not_contain('Alice')
  49. def test_slots(screen: Screen):
  50. with ui.table(columns=columns, rows=rows) as table:
  51. with table.add_slot('top-row'):
  52. with table.row():
  53. with table.cell():
  54. ui.label('This is the top slot.')
  55. table.add_slot('body', '''
  56. <q-tr :props="props">
  57. <q-td key="name" :props="props">overridden</q-td>
  58. <q-td key="age" :props="props">
  59. <q-badge color="green">{{ props.row.age }}</q-badge>
  60. </q-td>
  61. </q-tr>
  62. ''')
  63. screen.open('/')
  64. screen.should_contain('This is the top slot.')
  65. screen.should_not_contain('Alice')
  66. screen.should_contain('overridden')
  67. screen.should_contain('21')