Browse Source

testing auto-context with async/await

Rodja Trappe 2 years ago
parent
commit
ff39f6d729
2 changed files with 34 additions and 8 deletions
  1. 25 0
      tests/test_auto_context.py
  2. 9 8
      tests/user.py

+ 25 - 0
tests/test_auto_context.py

@@ -1,3 +1,5 @@
+import asyncio
+
 from nicegui import ui
 
 from .user import User
@@ -19,3 +21,26 @@ def test_adding_element_to_private_page(user: User):
     user.open('/')
     user.click('add label')
     user.should_see('added')
+
+
+def test_adding_elements_with_async_await(user: User):
+    async def add_a():
+        await asyncio.sleep(0.1)
+        ui.label('A')
+
+    async def add_b():
+        await asyncio.sleep(0.1)
+        ui.label('B')
+
+    with ui.card():
+        ui.timer(1.0, add_a, once=True)
+    with ui.card():
+        ui.timer(1.1, add_b, once=True)
+
+    user.open('/')
+    assert '''
+card
+  A
+card
+  B
+''' in user.page(), f'{user.page()} should show cards with "A" and "B"'

+ 9 - 8
tests/user.py

@@ -7,7 +7,7 @@ from selenium.common.exceptions import NoSuchElementException
 from selenium.webdriver.remote.webelement import WebElement
 
 PORT = 3392
-IGNORED_CLASSES = ['row', 'column', 'q-field', 'q-field__label', 'q-input']
+IGNORED_CLASSES = ['row', 'column', 'q-card', 'q-field', 'q-field__label', 'q-input']
 
 
 class User():
@@ -54,10 +54,11 @@ class User():
         except NoSuchElementException:
             raise AssertionError(f'Could not find "{text}" on:\n{self.page()}')
 
-    def page(self) -> str:
-        return f'Title: {self.selenium.title}\n\n' + self.content(self.selenium.find_element_by_tag_name('body'))
+    def page(self, with_extras: bool = False) -> str:
+        return f'Title: {self.selenium.title}\n\n' + \
+            self.content(self.selenium.find_element_by_tag_name('body'), with_extras=with_extras)
 
-    def content(self, element: WebElement, indent: str = '') -> str:
+    def content(self, element: WebElement, indent: str = '', with_extras: bool = False) -> str:
         content = ''
         classes: list[str] = []
         for child in element.find_elements_by_xpath('./*'):
@@ -70,8 +71,8 @@ class User():
                 content += f'{indent}{child.text}'
             classes = child.get_attribute('class').strip().split()
             if classes:
-                if classes[0] in ['row', 'column']:
-                    content += classes[0]
+                if classes[0] in ['row', 'column', 'q-card']:
+                    content += classes[0].removeprefix('q-')
                     is_element = True
                     is_group = True
                 if classes[0] == 'q-field':
@@ -87,12 +88,12 @@ class User():
                 [classes.remove(c) for c in IGNORED_CLASSES if c in classes]
                 for i, c in enumerate(classes):
                     classes[i] = c.removeprefix('q-field--')
-                if is_element:
+                if is_element and with_extras:
                     content += f' [class: {" ".join(classes)}]'
             if is_element:
                 content += '\n'
             if render_children:
-                content += self.content(child, indent + ('  ' if is_group else ''))
+                content += self.content(child, indent + ('  ' if is_group else ''), with_extras)
         return content
 
     def get_tags(self, name: str) -> list[WebElement]: