Skip to content

Commit

Permalink
Use Stere.retry_time when searching element attributes, raise better …
Browse files Browse the repository at this point in the history
…raise better error when it fails (#312)
  • Loading branch information
jsfehler authored Sep 25, 2020
1 parent 40f1d86 commit 56adce5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
15 changes: 14 additions & 1 deletion stere/fields/field.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import typing

import splinter

from stere import Stere

from .decorators import stere_performer
from .element_builder import build_element
from ..utils import _retry
Expand Down Expand Up @@ -63,7 +67,16 @@ def __getattr__(self, val):
raise AttributeError

# Try getting the attribute from the found element.
return getattr(self.find(), val)
try:
elem = self.find(Stere.retry_time)
except splinter.exceptions.ElementDoesNotExist as e:
msg = (
'Failed to get element attribute.'
f'Could not find element with {self.strategy}: {self.locator}' # NOQA E501
)
raise AttributeError(msg) from e

return getattr(elem, val)

def __repr__(self):
"""Provide a string representation of this class."""
Expand Down
20 changes: 20 additions & 0 deletions tests/splinter/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ def test_field_getattr_should_not_exist(test_page):
assert test_page.button.foobar()


def test_field_getattr_find_fails(test_page):
"""
When I try to access an attribute that does not exist from a Field
And the element is not found
Then an error is raised
And the correct error message is displayed
"""
test_page.navigate()

with pytest.raises(AttributeError) as e:
test_page.missing_button.does_not_exist

msg = (
'Failed to get element attribute.'
'Could not find element with data-test-id: not_on_the_page'
)

assert msg == str(e.value)


def test_non_unique_field_find(test_page):
"""
When I try to use find() on a Field that is found multiple times
Expand Down

0 comments on commit 56adce5

Please sign in to comment.