Skip to content

Commit

Permalink
zauberzeug#287 tests for binding with ui.input
Browse files Browse the repository at this point in the history
  • Loading branch information
rodja committed Feb 2, 2023
1 parent 061b388 commit 13f13c3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]'
Expand All @@ -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:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_binding.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

from selenium.webdriver.common.keys import Keys

from nicegui import ui

from .screen import Screen
Expand Down Expand Up @@ -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'

0 comments on commit 13f13c3

Please sign in to comment.