test_upload.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from pathlib import Path
  2. from typing import List
  3. from selenium.webdriver.common.by import By
  4. from nicegui import events, ui
  5. from .screen import Screen
  6. test_path1 = Path('tests/test_upload.py').resolve()
  7. test_path2 = Path('tests/test_scene.py').resolve()
  8. def test_uploading_text_file(screen: Screen):
  9. results: List[events.UploadEventArguments] = []
  10. ui.upload(on_upload=results.append, label='Test Title')
  11. screen.open('/')
  12. screen.should_contain('Test Title')
  13. screen.selenium.find_element(By.CLASS_NAME, 'q-uploader__input').send_keys(str(test_path1))
  14. screen.wait(0.1)
  15. screen.selenium.find_elements(By.CLASS_NAME, 'q-btn')[1].click()
  16. screen.wait(0.1)
  17. assert len(results) == 1
  18. assert results[0].name == test_path1.name
  19. assert results[0].type in {'text/x-python', 'text/x-python-script'}
  20. assert results[0].content.read() == test_path1.read_bytes()
  21. def test_two_upload_elements(screen: Screen):
  22. results: List[events.UploadEventArguments] = []
  23. ui.upload(on_upload=results.append, auto_upload=True, label='Test Title 1')
  24. ui.upload(on_upload=results.append, auto_upload=True, label='Test Title 2')
  25. screen.open('/')
  26. screen.should_contain('Test Title 1')
  27. screen.should_contain('Test Title 2')
  28. screen.selenium.find_element(By.CLASS_NAME, 'q-uploader__input').send_keys(str(test_path1))
  29. screen.selenium.find_elements(By.CLASS_NAME, 'q-uploader__input')[1].send_keys(str(test_path2))
  30. screen.wait(0.1)
  31. assert len(results) == 2
  32. assert results[0].name == test_path1.name
  33. assert results[1].name == test_path2.name
  34. def test_uploading_from_two_tabs(screen: Screen):
  35. @ui.page('/')
  36. def page():
  37. ui.upload(on_upload=lambda e: ui.label(f'uploaded {e.name}'), auto_upload=True)
  38. screen.open('/')
  39. screen.switch_to(1)
  40. screen.open('/')
  41. screen.should_not_contain(test_path1.name)
  42. screen.selenium.find_element(By.CLASS_NAME, 'q-uploader__input').send_keys(str(test_path1))
  43. screen.should_contain(f'uploaded {test_path1.name}')
  44. screen.switch_to(0)
  45. screen.should_not_contain(f'uploaded {test_path1.name}')
  46. def test_upload_with_header_slot(screen: Screen):
  47. with ui.upload().add_slot('header'):
  48. ui.label('Header')
  49. screen.open('/')
  50. screen.should_contain('Header')
  51. def test_replace_upload(screen: Screen):
  52. with ui.row() as container:
  53. ui.upload(label='A')
  54. def replace():
  55. container.clear()
  56. with container:
  57. ui.upload(label='B')
  58. ui.button('Replace', on_click=replace)
  59. screen.open('/')
  60. screen.should_contain('A')
  61. screen.click('Replace')
  62. screen.should_contain('B')
  63. screen.should_not_contain('A')
  64. def test_reset_upload(screen: Screen):
  65. upload = ui.upload()
  66. ui.button('Reset', on_click=upload.reset)
  67. screen.open('/')
  68. screen.selenium.find_element(By.CLASS_NAME, 'q-uploader__input').send_keys(str(test_path1))
  69. screen.should_contain(test_path1.name)
  70. screen.click('Reset')
  71. screen.wait(0.5)
  72. screen.should_not_contain(test_path1.name)