test_upload.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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}')