Skip to content

Commit

Permalink
Create visible_label locator to maintain backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
lmtierney committed Jul 25, 2018
1 parent 93c62ff commit 26ea6c3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
30 changes: 19 additions & 11 deletions nerodia/locators/element/locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,14 @@ def _create_normalized_selector(self, filter):

self.normalized_selector = self.selector_builder.normalized_selector

label_key = None
if 'label' in self.normalized_selector:
self._process_label()
label_key = 'label'
elif 'visible_label' in self.normalized_selector:
label_key = 'visible_label'

if label_key:
self._process_label(label_key)
if self.normalized_selector is None:
return None

Expand Down Expand Up @@ -183,11 +189,11 @@ def _create_filter_selector(self):

return self.filter_selector

def _process_label(self):
if isinstance(self.normalized_selector.get('label'), Pattern) and \
def _process_label(self, label_key):
if isinstance(self.normalized_selector.get(label_key), Pattern) and \
self.selector_builder.should_use_label_element:

label = self._label_from_text
label = self._label_from_text(label_key)
if not label:
self.normalized_selector = None
return None
Expand All @@ -198,13 +204,14 @@ def _process_label(self):
else:
self.driver_scope = label

@property
def _label_from_text(self):
def _label_from_text(self, label_key):
# TODO: this won't work correctly if @wd is a sub-element
# TODO: figure out how to do this with find_element
label_text = self.normalized_selector.pop('label', None)
label_text = self.normalized_selector.pop(label_key, None)
locator_key = label_key.replace('label', 'text')
elements = self._locate_elements('tag_name', 'label', self.driver_scope)
return next((e for e in elements if self._matches_selector(e, {'text': label_text})), None)
return next((e for e in elements if self._matches_selector(e, {locator_key: label_text})),
None)

def _matches_selector(self, element, selector):
def check_match(how, what):
Expand All @@ -222,9 +229,10 @@ def check_match(how, what):
_execute_js('getTextContent', element).strip()
text_content_matches = Validator.match_str_or_regex(text_selector, text_content)
if matches != text_content_matches:
nerodia.logger.deprecate(':text locator with RegExp: {!r} matched an element with '
'hidden text'.format(text_selector.pattern),
'visible_text')
key = 'text' if 'text' in self.selector else 'label'
nerodia.logger.deprecate('Using {!r} locator with RegExp: {!r} matched an element '
'with hidden text'.format(key, text_selector.pattern),
'visible_{}'.format(key))

return matches

Expand Down
4 changes: 4 additions & 0 deletions tests/browser/elements/text_field_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def test_returns_true_if_the_element_exists(self, browser):
assert browser.text_field(label=compile(r'(Last|First) name')).exists is True
assert browser.text_field(label='Without for').exists is True
assert browser.text_field(label=compile(r'Without for')).exists is True
assert browser.text_field(label='With hidden text').exists is True
assert browser.text_field(visible_label=compile(r'With text')).exists is True

def test_returns_the_first_text_field_if_given_no_args(self, browser):
assert browser.text_field().exists
Expand Down Expand Up @@ -62,6 +64,8 @@ def test_returns_false_if_the_element_does_not_exist(self, browser):
assert browser.text_field(xpath="//input[@id='no_such_id']").exists is False
assert browser.text_field(label='bad_label').exists is False
assert browser.text_field(label=compile(r'bad_label')).exists is False
assert browser.text_field(label='With text').exists is False
assert browser.text_field(visible_label=compile(r'With hidden text')).exists is False

# input type='hidden' should not be found by #text_field
assert browser.text_field(id='new_user_interests_dolls').exists is False
Expand Down

0 comments on commit 26ea6c3

Please sign in to comment.