Skip to content

Commit

Permalink
Add input text for active element
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosmko committed Feb 9, 2022
1 parent fae262c commit a61862f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ run.sh
.eggs/
.cache/
venv*/

.DS_Store
53 changes: 44 additions & 9 deletions AppiumLibrary/keywords/_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ def clear_text(self, locator):
See `introduction` for details about locating elements.
"""
self._info("Clear text field '%s'" % locator)
self._element_clear_text_by_locator(locator)
if locator is None:
self._info("Clear text field")
self._element_clear_text()
else:
self._info("Clear text field '%s'" % locator)
self._element_clear_text_by_locator(locator)

def click_element(self, locator):
"""Click element identified by `locator`.
Expand Down Expand Up @@ -66,8 +70,12 @@ def input_text(self, locator, text):
See `introduction` for details about locating elements.
"""
self._info("Typing text '%s' into text field '%s'" % (text, locator))
self._element_input_text_by_locator(locator, text)
if locator is None:
self._info("Typing text '%s' if keyboard is visible" % text)
self._element_active_input_text(text)
else:
self._info("Typing text '%s' into text field '%s'" % (text, locator))
self._element_input_text_by_locator(locator, text)

def input_password(self, locator, text):
"""Types the given password into text field identified by `locator`.
Expand All @@ -76,8 +84,12 @@ def input_password(self, locator, text):
does not log the given password. See `introduction` for details about
locating elements.
"""
self._info("Typing password into text field '%s'" % locator)
self._element_input_text_by_locator(locator, text)
if locator is None:
self._info("Typing password if keyboard is visible")
self._element_active_input_text(text)
else:
self._info("Typing password into text field '%s'" % locator)
self._element_input_text_by_locator(locator, text)

def input_value(self, locator, text):
"""Sets the given value into text field identified by `locator`. This is an IOS only keyword, input value makes use of set_value
Expand Down Expand Up @@ -579,13 +591,31 @@ def _element_clear_text_by_locator(self, locator):
except Exception as e:
raise e

def _element_clear_text(self):
try:
self._wait_keyboard()
driver = self._current_application()
element = driver.switch_to.active_element
element.clear()
except Exception as e:
raise e

def _element_input_text_by_locator(self, locator, text):
try:
element = self._element_find(locator, True, True)
element.send_keys(text)
except Exception as e:
raise e

def _element_active_input_text(self, text):
try:
self._wait_keyboard()
driver = self._current_application()
element = driver.switch_to.active_element
element.send_keys(text)
except Exception as e:
raise e

def _element_input_text_by_class_name(self, class_name, index_or_name, text):
try:
element = self._find_element_by_class_name(class_name, index_or_name)
Expand Down Expand Up @@ -650,9 +680,8 @@ def _get_text(self, locator):
return None

def _is_text_present(self, text):
text_norm = normalize('NFD', text)
source_norm = normalize('NFD', self.get_source())
return text_norm in source_norm
application = self._current_application()
return self._element_finder.find_text(application, text)

def _is_element_present(self, locator):
application = self._current_application()
Expand All @@ -664,3 +693,9 @@ def _is_visible(self, locator):
if element is not None:
return element.is_displayed()
return None

def _wait_keyboard(self):
# wait keyboard
self._bi.wait_until_keyword_succeeds(
'5 times', '50ms', self.is_keyboard_shown.__name__
)
2 changes: 1 addition & 1 deletion AppiumLibrary/keywords/_touch.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def tap(self, locator, x_offset=None, y_offset=None, count=1):
driver = self._current_application()
el = self._element_find(locator, True, True)
action = TouchAction(driver)
action.tap(el,x_offset,y_offset, count).perform()
action.tap(el, x_offset, y_offset, count).perform()

def tap_with_number_of_taps(self, locator, number_of_taps, number_of_touches):
""" Sends one or more taps with one or more touch points.iOS only.
Expand Down
6 changes: 6 additions & 0 deletions AppiumLibrary/locators/elementfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from AppiumLibrary import utils
from robot.api import logger
from unicodedata import normalize


class ElementFinder(object):
Expand Down Expand Up @@ -35,6 +36,11 @@ def find(self, browser, locator, tag=None):
(tag, constraints) = self._get_tag_and_constraints(tag)
return strategy(browser, criteria, tag, constraints)

def find_text(self, browser, text):
text_norm = normalize('NFD', text)
source_norm = normalize('NFD', browser.page_source)
return text_norm in source_norm

# Strategy routines, private

def _find_by_identifier(self, browser, criteria, tag, constraints):
Expand Down

0 comments on commit a61862f

Please sign in to comment.