Explorar o código

adding test for creating link by passing a page builder fuction

Rodja Trappe %!s(int64=2) %!d(string=hai) anos
pai
achega
dcd96da8dc
Modificáronse 2 ficheiros con 32 adicións e 9 borrados
  1. 18 2
      tests/test_pages.py
  2. 14 7
      tests/user.py

+ 18 - 2
tests/test_pages.py

@@ -1,17 +1,33 @@
 from nicegui import ui
 
+from .user import User
 
-async def test_title(user):
+
+async def test_title(user: User):
     @ui.page('/', title='My Custom Title')
     def page():
         ui.label('some content')
+
     user.open()
     user.should_see('My Custom Title')
 
 
-async def test_route_with_custom_path(user):
+async def test_route_with_custom_path(user: User):
     @ui.page('/test_route')
     def page():
         ui.label('page with custom path')
+
     user.open('/test_route')
     user.should_see('page with custom path')
+
+
+async def test_link_to_page_by_passing_function(user: User):
+    @ui.page('/subpage')
+    def page():
+        ui.label('the subpage')
+
+    ui.link('link to subpage', page)
+
+    user.open()
+    user.click('link to subpage')
+    user.should_see('the subpage')

+ 14 - 7
tests/user.py

@@ -4,33 +4,40 @@ from time import sleep
 
 from nicegui import globals as nicegui_globals
 from nicegui import ui
+from selenium import webdriver
 from selenium.webdriver.common.by import By
+from selenium.webdriver.remote.webelement import WebElement
 
 
 class User():
 
-    def __init__(self, selenium):
+    def __init__(self, selenium: webdriver.Chrome):
         self.selenium = selenium
         self.thread = None
 
-    def start_server(self):
+    def start_server(self) -> None:
         '''Start the webserver in a separate thread. This is the equivalent of `ui.run()` in a normal script.'''
         self.thread = threading.Thread(target=ui.run, kwargs={'port': 3392, 'show': False, 'reload': False})
         self.thread.start()
         sleep(1)
 
-    def stop_server(self):
+    def stop_server(self) -> None:
         '''Stop the webserver.'''
         nicegui_globals.server.should_exit = True
         self.thread.join()
 
-    def open(self, path: str = '/'):
+    def open(self, path: str = '/') -> None:
         if self.thread is None:
             self.start_server()
         self.selenium.get('http://localhost:3392' + path)
 
-    def should_see(self, text: str):
+    def should_see(self, text: str) -> None:
         if text == self.selenium.title:
             return
-        element = self.selenium.find_element(By.XPATH, f'//*[contains(text(),"{text}")]')
-        assert element.text == text
+        assert self.find(text).text == text
+
+    def click(self, target_text: str) -> None:
+        self.find(target_text).click()
+
+    def find(self, text: str) -> WebElement:
+        return self.selenium.find_element(By.XPATH, f'//*[contains(text(),"{text}")]')