Skip to content

Commit

Permalink
Merge pull request #265 from RedHatQE/fix-clear-input-number-type
Browse files Browse the repository at this point in the history
Fix clear input for number type
  • Loading branch information
LightOfHeaven1994 authored Sep 30, 2024
2 parents ee52664 + 4b8c05d commit d149b5d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/widgetastic/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,22 +856,28 @@ def middle_of(self, *args, **kwargs) -> Location:
location = self.location_of(*args, **kwargs)
return Location(int(location.x + size.width / 2), int(location.y + size.height / 2))

def clear(self, locator: LocatorAlias, *args, **kwargs) -> None:
def clear(self, locator: LocatorAlias, *args, **kwargs) -> bool:
"""Clears a text input with given locator."""
self.logger.debug("clear: %r", locator)

el = self.element(locator, *args, **kwargs)
self.plugin.before_keyboard_input(el, None)

self.click(locator, *args, **kwargs)
# CTRL + A doesn't work on 'number' types, as
# browser does not treat the numeric value as selectable text
if el.get_attribute("type") == "number":
self.execute_script("arguments[0].value = '';", el)
el.send_keys(Keys.SPACE, Keys.BACK_SPACE)

ActionChains(self.selenium).key_down(Keys.CONTROL).send_keys("a").key_up(
Keys.CONTROL
).perform()
result = el.send_keys(Keys.DELETE)
el.send_keys(Keys.DELETE)

self.plugin.after_keyboard_input(el, None)

return result
return el.get_attribute("value") == ""

def is_selected(self, *args, **kwargs) -> bool:
return self.element(*args, **kwargs).is_selected()
Expand Down
1 change: 1 addition & 0 deletions testing/html/testing_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2 style="display: none;" id="invisible">This is invisible</h2>
</select>
<input type='text' id='input' />
<input type='text' id='input_paste' />
<input type='number' id='input_number' />
<input type='file' id='fileinput' />
<input type='color' id='colourinput' />

Expand Down
7 changes: 7 additions & 0 deletions testing/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ def test_simple_input_send_keys_clear(browser):
assert browser.get_attribute("value", "#input") == ""


def test_clear_input_type_number(browser):
browser.send_keys("3", "#input_number")
assert browser.get_attribute("value", "#input_number") == "3"
browser.clear("#input_number")
assert browser.get_attribute("value", "#input") == ""


def test_copy_paste(browser):
t = "copy and paste text"
browser.send_keys(t, "#input")
Expand Down

0 comments on commit d149b5d

Please sign in to comment.