Browse Source

Fix some failing pytests (#3638)

* make dark mode test more robust

* make query body test more robust

* fix flaky scene test
Falko Schindler 8 months ago
parent
commit
fca6528270
4 changed files with 26 additions and 14 deletions
  1. 1 0
      nicegui/testing/screen_plugin.py
  2. 11 5
      tests/test_dark_mode.py
  3. 8 3
      tests/test_query.py
  4. 6 6
      tests/test_scene.py

+ 1 - 0
nicegui/testing/screen_plugin.py

@@ -23,6 +23,7 @@ DOWNLOAD_DIR = Path(__file__).parent / 'download'
 def nicegui_chrome_options(chrome_options: webdriver.ChromeOptions) -> webdriver.ChromeOptions:
     """Configure the Chrome options for the NiceGUI tests."""
     chrome_options.add_argument('disable-dev-shm-usage')
+    chrome_options.add_argument('disable-search-engine-choice-screen')
     chrome_options.add_argument('no-sandbox')
     chrome_options.add_argument('headless')
     chrome_options.add_argument('disable-gpu' if 'GITHUB_ACTIONS' in os.environ else '--use-gl=angle')

+ 11 - 5
tests/test_dark_mode.py

@@ -10,17 +10,23 @@ def test_dark_mode(screen: Screen):
     ui.button('Auto', on_click=dark.auto)
     ui.button('Toggle', on_click=dark.toggle)
 
+    def assert_dark(value: bool) -> None:
+        classes = (screen.find_by_tag('body').get_attribute('class') or '').split()
+        assert ('dark' in classes) == value
+        assert ('body--dark' in classes) == value
+        assert ('body--light' in classes) != value
+
     screen.open('/')
     screen.should_contain('Hello')
-    assert screen.find_by_tag('body').get_attribute('class') == 'desktop no-touch body--light'
+    assert_dark(False)
 
     screen.click('Dark')
     screen.wait(0.5)
-    assert screen.find_by_tag('body').get_attribute('class') == 'desktop no-touch body--dark dark'
+    assert_dark(True)
 
     screen.click('Auto')
     screen.wait(0.5)
-    assert screen.find_by_tag('body').get_attribute('class') == 'desktop no-touch body--light'
+    assert_dark(False)
 
     screen.click('Toggle')
     screen.wait(0.5)
@@ -28,8 +34,8 @@ def test_dark_mode(screen: Screen):
 
     screen.click('Light')
     screen.wait(0.5)
-    assert screen.find_by_tag('body').get_attribute('class') == 'desktop no-touch body--light'
+    assert_dark(False)
 
     screen.click('Toggle')
     screen.wait(0.5)
-    assert screen.find_by_tag('body').get_attribute('class') == 'desktop no-touch body--dark dark'
+    assert_dark(True)

+ 8 - 3
tests/test_query.py

@@ -1,3 +1,5 @@
+from typing import List
+
 from nicegui import ui
 from nicegui.testing import Screen
 
@@ -12,17 +14,20 @@ def test_query_body(screen: Screen):
     ui.button('Data X = 1', on_click=lambda: ui.query('body').props('data-x=1'))
     ui.button('Data X = 2', on_click=lambda: ui.query('body').props('data-x=2'))
 
+    def get_bg_classes() -> List[str]:
+        return [c for c in (screen.find_by_tag('body').get_attribute('class') or '').split() if c.startswith('bg-')]
+
     screen.open('/')
     screen.should_contain('Hello')
-    assert screen.find_by_tag('body').get_attribute('class') == 'desktop no-touch body--light bg-orange-100'
+    assert get_bg_classes() == ['bg-orange-100']
 
     screen.click('Red background')
     screen.wait(0.5)
-    assert screen.find_by_tag('body').get_attribute('class') == 'desktop no-touch body--light bg-red-100'
+    assert get_bg_classes() == ['bg-red-100']
 
     screen.click('Blue background')
     screen.wait(0.5)
-    assert screen.find_by_tag('body').get_attribute('class') == 'desktop no-touch body--light bg-blue-100'
+    assert get_bg_classes() == ['bg-blue-100']
 
     screen.click('Small padding')
     screen.wait(0.5)

+ 6 - 6
tests/test_scene.py

@@ -1,4 +1,4 @@
-from typing import Optional
+from typing import List
 
 import numpy as np
 from selenium.common.exceptions import JavascriptException
@@ -45,14 +45,14 @@ def test_no_object_duplication_on_index_client(screen: Screen):
 
 
 def test_no_object_duplication_with_page_builder(screen: Screen):
-    scene: Optional[ui.scene] = None
+    scene_ids: List[int] = []
 
     @ui.page('/')
     def page():
-        nonlocal scene
         with ui.scene() as scene:
             sphere = scene.sphere().move(0, -4, 0)
             ui.timer(0.1, lambda: sphere.move(0, sphere.y + 0.5, 0))
+        scene_ids.append(scene.id)
 
     screen.open('/')
     screen.wait(0.4)
@@ -60,10 +60,10 @@ def test_no_object_duplication_with_page_builder(screen: Screen):
     screen.open('/')
     screen.switch_to(0)
     screen.wait(0.2)
-    assert scene
-    assert screen.selenium.execute_script(f'return scene_c{scene.id}.children.length') == 5
+    assert screen.selenium.execute_script(f'return scene_c{scene_ids[0]}.children.length') == 5
     screen.switch_to(1)
-    assert screen.selenium.execute_script(f'return scene_c{scene.id}.children.length') == 5
+    screen.wait(0.2)
+    assert screen.selenium.execute_script(f'return scene_c{scene_ids[1]}.children.length') == 5
 
 
 def test_deleting_group(screen: Screen):