diff --git a/tests/screen.py b/tests/screen.py index e6ec3e9bb..f82c8869b 100644 --- a/tests/screen.py +++ b/tests/screen.py @@ -96,6 +96,11 @@ def click_at_position(self, element: WebElement, x: int, y: int) -> None: action = ActionChains(self.selenium) action.move_to_element_with_offset(element, x, y).click().perform() + def type(self, text: str) -> None: + self.selenium.execute_script("window.focus();") + self.wait(0.2) + self.selenium.switch_to.active_element.send_keys(text) + def find(self, text: str) -> WebElement: try: query = f'//*[not(self::script) and not(self::style) and contains(text(), "{text}")]' @@ -106,6 +111,9 @@ def find(self, text: str) -> WebElement: raise AssertionError(f'Found "{text}" but it is hidden') return element except NoSuchElementException as e: + for input in self.selenium.find_elements(By.TAG_NAME, 'input'): + if input.get_attribute('value') == text: + return input raise AssertionError(f'Could not find "{text}"') from e def render_js_logs(self) -> str: diff --git a/tests/test_binding.py b/tests/test_binding.py index 049a0d836..627e593d2 100644 --- a/tests/test_binding.py +++ b/tests/test_binding.py @@ -1,4 +1,6 @@ +from selenium.webdriver.common.keys import Keys + from nicegui import ui from .screen import Screen @@ -59,3 +61,25 @@ class Model: screen.should_contain('2,2') screen.should_not_contain('1,1') assert data.selection == [2, 2] + + +def test_binding_to_input(screen: Screen): + class Model: + text = 'one' + data = Model() + input = ui.input().bind_value(data, 'text') + + screen.open('/') + screen.should_contain('one') + screen.type(Keys.TAB) + screen.type('two') + screen.should_contain('two') + assert data.text == 'two' + data.text = 'three' + screen.should_contain('three') + input.set_value('four') + screen.should_contain('four') + assert data.text == 'four' + input.value = 'five' + screen.should_contain('five') + assert data.text == 'five'