test_table.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.wait(0.5)
  49. screen.should_not_contain('Alice')
  50. def test_slots(screen: Screen):
  51. with ui.table(columns=columns, rows=rows) as table:
  52. with table.add_slot('top-row'):
  53. with table.row():
  54. with table.cell():
  55. ui.label('This is the top slot.')
  56. table.add_slot('body', '''
  57. <q-tr :props="props">
  58. <q-td key="name" :props="props">overridden</q-td>
  59. <q-td key="age" :props="props">
  60. <q-badge color="green">{{ props.row.age }}</q-badge>
  61. </q-td>
  62. </q-tr>
  63. ''')
  64. screen.open('/')
  65. screen.should_contain('This is the top slot.')
  66. screen.should_not_contain('Alice')
  67. screen.should_contain('overridden')
  68. screen.should_contain('21')