Browse Source

pytest: began more elaborate "user.should_see".
Instead of querying xpaths etc the new strategy creates a string representation of the page which can then be displayed and asserted with regex etc.

Rodja Trappe 2 years ago
parent
commit
3759a6f13e
2 changed files with 53 additions and 4 deletions
  1. 25 0
      tests/test_user.py
  2. 28 4
      tests/user.py

+ 25 - 0
tests/test_user.py

@@ -0,0 +1,25 @@
+from nicegui import ui
+
+from .user import User
+
+
+def test_rendering_page(user: User):
+    ui.label('test label')
+    with ui.row().classes('positive'):
+        ui.input('test input', placeholder='test placeholder')
+    with ui.column():
+        ui.label('1')
+        ui.label('2')
+        ui.label('3')
+
+    user.open('/')
+    assert user.page() == '''Title: NiceGUI
+
+test label
+row
+  test input: test placeholder
+column
+  1
+  2
+  3
+'''

+ 28 - 4
tests/user.py

@@ -42,7 +42,7 @@ class User():
                     raise RuntimeError('The NiceGUI server has stopped running')
 
     def should_see(self, text: str) -> None:
-        assert self.selenium.title == text or self.find(text).text == text
+        assert text in self.page(), f'Could not find "{text}" on:\n{self.page()}'
 
     def click(self, target_text: str) -> None:
         self.find(target_text).click()
@@ -51,10 +51,34 @@ class User():
         try:
             return self.selenium.find_element_by_xpath(f'//*[contains(text(),"{text}")]')
         except NoSuchElementException:
-            raise AssertionError(f'Could not find "{text}" on:\n{self.get_body()}')
+            raise AssertionError(f'Could not find "{text}" on:\n{self.page()}')
 
-    def get_body(self) -> str:
-        return self.selenium.find_element_by_tag_name('body').text
+    def page(self) -> str:
+        return f'Title: {self.selenium.title}\n\n' + self.content(self.selenium.find_element_by_tag_name('body'))
+
+    def content(self, element: WebElement, indent: str = '') -> str:
+        content = ''
+        is_group = False
+        for child in element.find_elements_by_xpath('./*'):
+            assert isinstance(child, WebElement)
+            classes = child.get_attribute('class').split(' ')
+            if not child.find_elements_by_xpath('./*') and child.text:
+                content += f'{indent}{child.text}\n'
+            if classes and classes[0] in ['row', 'column']:
+                content += f'{classes[0]}\n'
+                is_group = True
+
+            if classes and classes[0] == 'q-field':
+                try:
+                    name = child.find_element_by_class_name('q-field__label').text
+                except NoSuchElementException:
+                    name = ''
+                input = child.find_element_by_tag_name('input')
+                value = input.get_attribute('value') or input.get_attribute('placeholder')
+                content += f'{indent}{name}: {value}\n'
+            else:
+                content += self.content(child, indent + ('  ' if is_group else ''))
+        return content
 
     def get_tags(self, name: str) -> list[WebElement]:
         return self.selenium.find_elements_by_tag_name(name)