test_input.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. 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. element.send_keys(' Doe')
  46. screen.wait(0.5)
  47. screen.should_not_contain('Too short')
  48. def test_input_with_multi_word_error_message(screen: Screen):
  49. input = ui.input(label='some input')
  50. ui.button('set error', on_click=lambda: input.props('error error-message="Some multi word error message"'))
  51. screen.open('/')
  52. screen.should_not_contain('Some multi word error message')
  53. screen.click('set error')
  54. screen.should_contain('Some multi word error message')
  55. def test_autocompletion(screen: Screen):
  56. ui.input('Input', autocomplete=['foo', 'bar', 'baz'])
  57. screen.open('/')
  58. element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Input"]')
  59. element.send_keys('f')
  60. screen.should_contain('oo')
  61. element.send_keys('l')
  62. screen.wait(0.5)
  63. screen.should_not_contain('oo')
  64. element.send_keys(Keys.BACKSPACE)
  65. screen.should_contain('oo')
  66. element.send_keys(Keys.TAB)
  67. screen.wait(0.2)
  68. assert element.get_attribute('value') == 'foo'
  69. element.send_keys(Keys.BACKSPACE)
  70. screen.wait(0.2)
  71. element.send_keys(Keys.BACKSPACE)
  72. screen.wait(0.2)
  73. element.send_keys('x')
  74. screen.wait(0.2)
  75. element.send_keys(Keys.TAB)
  76. screen.wait(0.5)
  77. assert element.get_attribute('value') == 'fx'