Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 23, 2023. It is now read-only.

Commit 9eeac97

Browse files
feat: Add get_element_by_descendant and get_element_by_ancestor
1 parent 1027dd7 commit 9eeac97

File tree

2 files changed

+103
-19
lines changed

2 files changed

+103
-19
lines changed

‎AppiumFlutterLibrary/finder/elementfinder.py‎

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from appium_flutter_finder import FlutterElement, FlutterFinder
2+
from typing import Literal, overload
23

34
class ElementFinder():
45
def __init__(self):
@@ -13,7 +14,24 @@ def __init__(self):
1314
'default': self._find_by_key,
1415
}
1516

16-
def find(self, application, locator):
17+
@overload
18+
def find(self, application, locator, serialized: Literal[False] = False) -> FlutterElement:
19+
...
20+
21+
@overload
22+
def find(self, application, locator, serialized: Literal[True]) -> str:
23+
...
24+
25+
@overload
26+
def find(self, application, locator, serialized: bool) -> FlutterElement | str:
27+
...
28+
29+
def find(self, application, locator, serialized: bool = False) -> FlutterElement | str:
30+
"""Finds the element
31+
32+
Params:
33+
serialized: Returns the serialized str of the element
34+
"""
1735
assert application is not None
1836
assert application is not None and len(locator) > 0
1937

@@ -23,41 +41,65 @@ def find(self, application, locator):
2341
strategy = self._strategies.get(prefix)
2442
if strategy is None:
2543
raise ValueError("Element locator with prefix '" + prefix + "' is not supported")
26-
return strategy(application, criteria)
44+
return strategy(application, criteria, serialized=serialized)
2745

28-
def _find_by_key(self, application, element_key):
46+
def _find_by_key(self, application, element_key, serialized=False):
2947
finder_key = self._element_finder.by_value_key(element_key)
48+
49+
if serialized:
50+
return finder_key
51+
3052
element = FlutterElement(application, finder_key)
3153

3254
return element
3355

34-
def _find_by_xpath(self, application, xpath):
56+
def _find_by_xpath(self, application, xpath, **kwargs):
3557
return
3658

37-
def _find_by_text(self, application, element_text):
59+
def _find_by_text(self, application, element_text, **kwargs):
3860
finder_text = self._element_finder.by_text(element_text)
3961
element = FlutterElement(application, finder_text)
4062

4163
return element
4264

43-
def _find_by_semantics_label(self, application, semantics):
65+
def _find_by_semantics_label(self, application, semantics, **kwargs):
4466
finder_semantics_label = self._element_finder.by_semantics_label(semantics)
4567
element = FlutterElement(application, finder_semantics_label)
4668

4769
return element
4870

49-
def _find_by_tooltip_message(self, application, tooltip):
71+
def _find_by_tooltip_message(self, application, tooltip, **kwargs):
5072
finder_tooltip_message = self._element_finder.by_tooltip(tooltip)
5173
element = FlutterElement(application, finder_tooltip_message)
5274

5375
return element
5476

55-
def _find_by_type(self, application, type):
77+
def _find_by_type(self, application, type, **kwargs):
5678
finder_type = self._element_finder.by_type(type)
5779
element = FlutterElement(application, finder_type)
5880

5981
return element
6082

83+
def _get_descendant(self, application, of, matching) -> FlutterElement:
84+
finder_descendant = self._element_finder.by_descendant(
85+
self.find(application, of, serialized=True),
86+
self.find(application, matching, serialized=True)
87+
)
88+
89+
element = FlutterElement(application, finder_descendant)
90+
91+
return element
92+
93+
def _get_ancestor(self, application, of, matching) -> FlutterElement:
94+
finder_descendant = self._element_finder.by_ancestor(
95+
self.find(application, of, serialized=True),
96+
self.find(application, matching, serialized=True)
97+
)
98+
99+
element = FlutterElement(application, finder_descendant)
100+
101+
return element
102+
61103
def _parse_locator(self, locator):
62104
prefix = None
63105
criteria = locator

‎AppiumFlutterLibrary/keywords/_element.py‎

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from appium.webdriver.webelement import WebElement
22
from .keywordgroup import KeywordGroup
33
from AppiumFlutterLibrary.finder import ElementFinder
4+
from appium_flutter_finder import FlutterElement
5+
from typing import Optional
46

57
def isstr(s):
68
return isinstance(s, str)
@@ -35,48 +37,64 @@ def clear_text(self, locator):
3537
except Exception as err:
3638
raise err
3739

38-
def click_element(self, locator):
40+
def click_element(self, locator: str|FlutterElement) ->None:
3941
"""Tap the specified element.
4042
4143
If the element does not support tapping an error
4244
will be raised.
4345
"""
44-
element = self._find_element(locator)
46+
if isinstance(locator, FlutterElement):
47+
element = locator
48+
else:
49+
element = self._find_element(locator)
50+
4551
self._info("Clicking on element %s" % locator)
4652
try:
4753
element.click()
4854
except Exception as err:
4955
raise err
5056

51-
def element_should_be_visible(self, locator):
57+
def element_should_be_visible(self, locator: str|FlutterElement):
5258
"""Verify if the element is visible using FlutterDriver.waitFor().
5359
5460
If the element isn't visible raise an Assertion Error.
5561
"""
56-
element = self._find_element(locator)
62+
if isinstance(locator, FlutterElement):
63+
element = locator
64+
else:
65+
element = self._find_element(locator)
66+
5767
if not self._is_visible(element):
5868
raise AssertionError("Element '%s' should be visible but not" % locator)
5969

60-
def element_text_should_be(self, locator, text):
70+
def element_text_should_be(self, locator: str|FlutterElement, text):
6171
"""Verify if the element text is equal to provided text.
6272
6373
If the text isn't equal raise an Assertion Error.
6474
6575
If the element does not support getText() raise an error.
6676
"""
67-
element = self._find_element(locator)
77+
if isinstance(locator, FlutterElement):
78+
element = locator
79+
else:
80+
element = self._find_element(locator)
81+
6882
if element.text != text:
6983
raise AssertionError("Element '%s' text should be '%s' but is '%s'." %
7084
(locator, text, element.text))
7185

72-
def element_text_should_not_be(self, locator, text):
86+
def element_text_should_not_be(self, locator: str|FlutterElement, text):
7387
"""Verify if the element text is not equal to provided text.
7488
7589
If the text isn't equal raise an Assertion Error.
7690
7791
If the element does not support getText() raise an error.
7892
"""
79-
element = self._find_element(locator)
93+
if isinstance(locator, FlutterElement):
94+
element = locator
95+
else:
96+
element = self._find_element(locator)
97+
8098
if element.text == text:
8199
raise AssertionError("Element '%s' text should not be '%s' but is." %
82100
(locator, text))
@@ -86,7 +104,7 @@ def get_element(self, locator):
86104
"""
87105
return self._find_element(locator)
88106

89-
def get_element_text(self, locator):
107+
def get_element_text(self, locator: str|FlutterElement):
90108
"""Returns element text
91109
92110
If the element does not support getText() raise an error.
@@ -95,6 +113,26 @@ def get_element_text(self, locator):
95113
self._info("Element '%s' text is '%s' " % (locator, text))
96114
return text
97115

116+
def get_element_descendant(self, of: str, matching: str) -> FlutterElement:
117+
"""Returns the element's descendant
118+
119+
Params:
120+
of: locator for the parent element
121+
matching: locator for the child element
122+
"""
123+
application = self._current_application()
124+
return self._element_finder._get_descendant(application, of, matching)
125+
126+
def get_element_ancestor(self, of: str, matching: str) -> FlutterElement:
127+
"""Returns the element's ancestor
128+
129+
Params:
130+
of: locator for the parent element
131+
matching: locator for the child element
132+
"""
133+
applicaiton = self.current_application()
134+
return self._element_finder._get_ancestor(applicaiton, of, matching)
135+
98136
def _is_visible(self, element):
99137
application = self._current_application()
100138
application.execute_script('flutter:waitFor', element.id, 1)
@@ -104,8 +142,12 @@ def _find_element(self, locator):
104142
application = self._current_application()
105143
return self._element_finder.find(application, locator)
106144

107-
def _get_text(self, locator):
108-
element = self._find_element(locator)
145+
def _get_text(self, locator: str | FlutterElement) -> Optional[str]:
146+
if isinstance(locator, FlutterElement):
147+
element = locator
148+
else:
149+
element = self._find_element(locator)
150+
109151
if element is not None:
110152
return element.text
111153
return None

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /