test_input.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from selenium.webdriver.common.by import By
  2. from selenium.webdriver.common.keys import Keys
  3. from nicegui import ui
  4. from .screen import Screen
  5. def test_input(screen: Screen):
  6. ui.input('Your name', value='John Doe')
  7. screen.open('/')
  8. screen.should_contain('Your name')
  9. element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Your name"]')
  10. assert element.get_attribute('type') == 'text'
  11. assert element.get_attribute('value') == 'John Doe'
  12. element.send_keys(' Jr.')
  13. assert element.get_attribute('value') == 'John Doe Jr.'
  14. def test_password(screen: Screen):
  15. ui.input('Your password', value='123456', password=True)
  16. screen.open('/')
  17. screen.should_contain('Your password')
  18. element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Your password"]')
  19. assert element.get_attribute('type') == 'password'
  20. assert element.get_attribute('value') == '123456'
  21. element.send_keys('789')
  22. screen.wait(0.5)
  23. assert element.get_attribute('value') == '123456789'
  24. def test_toggle_button(screen: Screen):
  25. ui.input('Your password', value='123456', password=True, password_toggle_button=True)
  26. screen.open('/')
  27. screen.should_contain('Your password')
  28. screen.should_contain('visibility_off')
  29. element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Your password"]')
  30. assert element.get_attribute('type') == 'password'
  31. assert element.get_attribute('value') == '123456'
  32. screen.click('visibility_off')
  33. screen.wait(0.5)
  34. assert element.get_attribute('type') == 'text'
  35. screen.click('visibility')
  36. screen.wait(0.5)
  37. assert element.get_attribute('type') == 'password'
  38. def test_input_validation(screen: Screen):
  39. input = ui.input('Name', validation={'Too short': lambda value: len(value) >= 5})
  40. screen.open('/')
  41. screen.should_contain('Name')
  42. element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Name"]')
  43. element.send_keys('John')
  44. screen.should_contain('Too short')
  45. assert input.error == 'Too short'
  46. element.send_keys(' Doe')
  47. screen.wait(0.5)
  48. screen.should_not_contain('Too short')
  49. assert input.error is None
  50. def test_input_with_multi_word_error_message(screen: Screen):
  51. input = ui.input(label='some input')
  52. ui.button('set error', on_click=lambda: input.props('error error-message="Some multi word error message"'))
  53. screen.open('/')
  54. screen.should_not_contain('Some multi word error message')
  55. screen.click('set error')
  56. screen.should_contain('Some multi word error message')
  57. def test_autocompletion(screen: Screen):
  58. input = ui.input('Input', autocomplete=['foo', 'bar', 'baz'])
  59. screen.open('/')
  60. element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Input"]')
  61. element.send_keys('f')
  62. screen.should_contain('oo')
  63. element.send_keys('l')
  64. screen.wait(0.5)
  65. screen.should_not_contain('oo')
  66. element.send_keys(Keys.BACKSPACE)
  67. screen.should_contain('oo')
  68. element.send_keys(Keys.TAB)
  69. screen.wait(0.2)
  70. assert element.get_attribute('value') == 'foo'
  71. assert input.value == 'foo'
  72. element.send_keys(Keys.BACKSPACE)
  73. element.send_keys(Keys.BACKSPACE)
  74. element.send_keys('x')
  75. element.send_keys(Keys.TAB)
  76. screen.wait(0.5)
  77. assert element.get_attribute('value') == 'fx'
  78. assert input.value == 'fx'
  79. input.set_autocomplete(['one', 'two'])
  80. element.send_keys(Keys.BACKSPACE)
  81. element.send_keys(Keys.BACKSPACE)
  82. element.send_keys('o')
  83. screen.should_contain('ne')
  84. def test_clearable_input(screen: Screen):
  85. input = ui.input(value='foo').props('clearable')
  86. ui.label().bind_text_from(input, 'value', lambda value: f'value: {value}')
  87. screen.open('/')
  88. screen.should_contain('value: foo')
  89. screen.click('cancel')
  90. screen.should_contain('value: None')
  91. def test_update_input(screen: Screen):
  92. input = ui.input('Name', value='Pete')
  93. screen.open('/')
  94. element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Name"]')
  95. assert element.get_attribute('value') == 'Pete'
  96. element.send_keys('r')
  97. screen.wait(0.5)
  98. assert element.get_attribute('value') == 'Peter'
  99. input.value = 'Pete'
  100. screen.wait(0.5)
  101. assert element.get_attribute('value') == 'Pete'
  102. def test_switching_focus(screen: Screen):
  103. input1 = ui.input()
  104. input2 = ui.input()
  105. ui.button('focus 1', on_click=lambda: input1.run_method('focus'))
  106. ui.button('focus 2', on_click=lambda: input2.run_method('focus'))
  107. screen.open('/')
  108. elements = screen.selenium.find_elements(By.XPATH, '//input')
  109. assert len(elements) == 2
  110. screen.click('focus 1')
  111. screen.wait(0.3)
  112. assert elements[0] == screen.selenium.switch_to.active_element
  113. screen.click('focus 2')
  114. screen.wait(0.3)
  115. assert elements[1] == screen.selenium.switch_to.active_element