Преглед изворни кода

#779 test + hotfix for replacing ui.link elements

Rodja Trappe пре 2 година
родитељ
комит
20eafd587b
3 измењених фајлова са 25 додато и 4 уклоњено
  1. 1 0
      nicegui/elements/link.py
  2. 8 4
      tests/screen.py
  3. 16 0
      tests/test_link.py

+ 1 - 0
nicegui/elements/link.py

@@ -26,6 +26,7 @@ class Link(TextElement):
         self._props['href'] = target if isinstance(target, str) else globals.page_routes[target]
         self._props['target'] = '_blank' if new_tab else '_self'
         self._classes = ['nicegui-link']
+        self._props['key'] = self.id  # HACK: workaround for #600
 
 
 class LinkTarget(Element):

+ 8 - 4
tests/screen.py

@@ -6,7 +6,8 @@ from typing import List
 
 import pytest
 from selenium import webdriver
-from selenium.common.exceptions import ElementNotInteractableException, NoSuchElementException
+from selenium.common.exceptions import (ElementNotInteractableException, NoSuchElementException,
+                                        StaleElementReferenceException)
 from selenium.webdriver import ActionChains
 from selenium.webdriver.common.by import By
 from selenium.webdriver.remote.webelement import WebElement
@@ -126,10 +127,13 @@ class Screen:
         try:
             query = f'//*[not(self::script) and not(self::style) and text()[contains(., "{text}")]]'
             element = self.selenium.find_element(By.XPATH, query)
-            if not element.is_displayed():
-                self.wait(0.1)  # HACK: repeat check after a short delay to avoid timing issue on fast machines
+            try:
                 if not element.is_displayed():
-                    raise AssertionError(f'Found "{text}" but it is hidden')
+                    self.wait(0.1)  # HACK: repeat check after a short delay to avoid timing issue on fast machines
+                    if not element.is_displayed():
+                        raise AssertionError(f'Found "{text}" but it is hidden')
+            except StaleElementReferenceException:
+                raise AssertionError(f'Found "{text}" but it is hidden')
             return element
         except NoSuchElementException as e:
             raise AssertionError(f'Could not find "{text}"') from e

+ 16 - 0
tests/test_link.py

@@ -34,3 +34,19 @@ def test_opening_link_in_new_tab(screen: Screen):
     screen.switch_to(0)
     screen.should_not_contain('the sub-page')
     screen.should_contain('open sub-page')
+
+
+def test_replacing_link(screen: Screen):
+    def change():
+        content.clear()
+        with content:
+            ui.link('zauberzeug', 'https://zauberzeug.com')
+    with ui.row() as content:
+        ui.link('nicegui.io', 'https://nicegui.io')
+    ui.button('change link', on_click=change)
+
+    screen.open('/')
+    content.clear()
+    screen.should_not_contain('nicegui')
+    screen.click('change link')
+    assert screen.find('zauberzeug').get_attribute('href') == 'https://zauberzeug.com/'