Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clear input for number type #265

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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