瀏覽代碼

began with first selenium pytest

Rodja Trappe 2 年之前
父節點
當前提交
4f500e88d1
共有 10 個文件被更改,包括 77 次插入4 次删除
  1. 1 2
      .vscode/settings.json
  2. 1 0
      nicegui/config.py
  3. 4 2
      nicegui/page.py
  4. 1 0
      nicegui/run.py
  5. 11 0
      tests/README.md
  6. 0 0
      tests/__init__.py
  7. 34 0
      tests/conftest.py
  8. 3 0
      tests/pytest.ini
  9. 3 0
      tests/requirements.txt
  10. 19 0
      tests/test_pages.py

+ 1 - 2
.vscode/settings.json

@@ -6,9 +6,8 @@
   "python.formatting.provider": "autopep8",
   "python.formatting.autopep8Args": ["--max-line-length=120", "--experimental"],
   "python.sortImports.args": ["--line-length=120"],
-  "python.testing.promptToConfigure": false,
   "python.testing.pytestArgs": ["."],
-  "python.testing.pytestEnabled": false,
+  "python.testing.pytestEnabled": true,
   "python.testing.unittestEnabled": false,
   "[python]": {
     "editor.defaultFormatter": "ms-python.python",

+ 1 - 0
nicegui/config.py

@@ -9,6 +9,7 @@ class Config():
     host: str = os.environ.get('HOST', '0.0.0.0')
     port: int = int(os.environ.get('PORT', '8080'))
     title: str = 'NiceGUI'
+    reload: bool = True
     favicon: str = 'favicon.ico'
     dark: Optional[bool] = False
     main_page_classes: str = 'q-ma-md column items-start'

+ 4 - 2
nicegui/page.py

@@ -203,8 +203,10 @@ def get_current_view() -> jp.HTMLBaseComponent:
 
 
 def error404() -> jp.QuasarPage:
-    wp = jp.QuasarPage(title=globals.config.title, favicon=globals.config.favicon,
-                       dark=globals.config.dark, tailwind=True)
+    title = globals.config.title if globals.config else '404'
+    favicon = globals.config.favicon if globals.config else None
+    dark = globals.config.dark if globals.config else False
+    wp = jp.QuasarPage(title=title, favicon=favicon, dark=dark, tailwind=True)
     div = jp.Div(a=wp, classes='py-20 text-center')
     jp.Div(a=div, classes='text-8xl py-5', text='☹',
            style='font-family: "Arial Unicode MS", "Times New Roman", Times, serif;')

+ 1 - 0
nicegui/run.py

@@ -32,6 +32,7 @@ def run(self, *,
         host=host,
         port=port,
         title=title,
+        reload=reload,
         favicon=favicon,
         dark=dark,
         main_page_classes=main_page_classes,

+ 11 - 0
tests/README.md

@@ -0,0 +1,11 @@
+# Automated Tests for NiceGUI
+
+## Setup
+
+### Mac
+
+```bash
+cd nicegui # enter the project root dir
+brew install cask chromedriver
+python3 -m pip install tests/requirements.txt
+```

+ 0 - 0
tests/__init__.py


+ 34 - 0
tests/conftest.py

@@ -0,0 +1,34 @@
+import datetime
+import os
+
+import pytest
+
+
+@pytest.fixture
+def chrome_options(chrome_options):
+    chrome_options.add_argument('headless')
+    chrome_options.add_argument('disable-gpu')
+    chrome_options.add_argument('window-size=1200x600')
+    return chrome_options
+
+
+@pytest.fixture
+def selenium(selenium):
+    selenium.implicitly_wait(0.1)
+    return selenium
+
+
+# original taken from https://github.com/theserverlessway/pytest-chrome/blob/master/tests/conftest.py
+@pytest.fixture
+def screenshot(selenium):
+    def shot(name=''):
+        directory = 'screenshots'
+        if not os.path.exists(directory):
+            os.makedirs(directory)
+        identifier = datetime.datetime.now().isoformat()
+        if name:
+            identifier = f'{identifier}-{name}'
+        filename = f'{directory}/{identifier}.png'
+        print(f'Storing Screenshot to {filename}')
+        selenium.get_screenshot_as_file(filename)
+    return shot

+ 3 - 0
tests/pytest.ini

@@ -0,0 +1,3 @@
+[pytest]
+addopts = --driver Chrome
+asyncio_mode = auto

+ 3 - 0
tests/requirements.txt

@@ -0,0 +1,3 @@
+selenium
+pytest-selenium
+pytest

+ 19 - 0
tests/test_pages.py

@@ -0,0 +1,19 @@
+import threading
+from time import sleep
+
+from nicegui import globals, ui
+
+
+async def test_title(selenium):
+    @ui.page('/', title='My Custom Title')
+    def page():
+        ui.label('hello world')
+    # start new thread
+    thread = threading.Thread(target=ui.run, kwargs={'port': 3392, 'show': False, 'reload': False})
+    thread.start()
+    sleep(2)
+    selenium.get('http://localhost:3392')
+    assert "My Custom Title" in selenium.title
+    print(globals)
+    globals.server.should_exit = True
+    thread.join()