test_input.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. from selenium.webdriver.common.by import By
  2. from selenium.webdriver.common.keys import Keys
  3. from nicegui import ui
  4. from nicegui.testing 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. assert not input_.validate()
  47. element.send_keys(' Doe')
  48. screen.wait(0.5)
  49. screen.should_not_contain('Too short')
  50. assert input_.error is None
  51. assert input_.validate()
  52. def test_input_with_multi_word_error_message(screen: Screen):
  53. input_ = ui.input(label='some input')
  54. ui.button('set error', on_click=lambda: input_.props('error error-message="Some multi word error message"'))
  55. screen.open('/')
  56. screen.should_not_contain('Some multi word error message')
  57. screen.click('set error')
  58. screen.should_contain('Some multi word error message')
  59. def test_autocompletion(screen: Screen):
  60. input_ = ui.input('Input', autocomplete=['foo', 'bar', 'baz'])
  61. screen.open('/')
  62. element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Input"]')
  63. element.send_keys('f')
  64. screen.should_contain('oo')
  65. element.send_keys('l')
  66. screen.wait(0.5)
  67. screen.should_not_contain('oo')
  68. element.send_keys(Keys.BACKSPACE)
  69. screen.should_contain('oo')
  70. element.send_keys(Keys.TAB)
  71. screen.wait(0.2)
  72. assert element.get_attribute('value') == 'foo'
  73. assert input_.value == 'foo'
  74. element.send_keys(Keys.BACKSPACE)
  75. element.send_keys(Keys.BACKSPACE)
  76. element.send_keys('x')
  77. element.send_keys(Keys.TAB)
  78. screen.wait(0.5)
  79. assert element.get_attribute('value') == 'fx'
  80. assert input_.value == 'fx'
  81. input_.set_autocomplete(['one', 'two'])
  82. element.send_keys(Keys.BACKSPACE)
  83. element.send_keys(Keys.BACKSPACE)
  84. element.send_keys('o')
  85. screen.should_contain('ne')
  86. def test_clearable_input(screen: Screen):
  87. input_ = ui.input(value='foo').props('clearable')
  88. ui.label().bind_text_from(input_, 'value', lambda value: f'value: {value}')
  89. screen.open('/')
  90. screen.should_contain('value: foo')
  91. screen.click('cancel')
  92. screen.should_contain('value: None')
  93. def test_update_input(screen: Screen):
  94. input_ = ui.input('Name', value='Pete')
  95. screen.open('/')
  96. element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Name"]')
  97. assert element.get_attribute('value') == 'Pete'
  98. element.send_keys('r')
  99. screen.wait(0.5)
  100. assert element.get_attribute('value') == 'Peter'
  101. input_.value = 'Pete'
  102. screen.wait(0.5)
  103. assert element.get_attribute('value') == 'Pete'
  104. def test_switching_focus(screen: Screen):
  105. input1 = ui.input()
  106. input2 = ui.input()
  107. ui.button('focus 1', on_click=lambda: input1.run_method('focus'))
  108. ui.button('focus 2', on_click=lambda: input2.run_method('focus'))
  109. screen.open('/')
  110. elements = screen.selenium.find_elements(By.XPATH, '//input')
  111. assert len(elements) == 2
  112. screen.click('focus 1')
  113. screen.wait(0.3)
  114. assert elements[0] == screen.selenium.switch_to.active_element
  115. screen.click('focus 2')
  116. screen.wait(0.3)
  117. assert elements[1] == screen.selenium.switch_to.active_element