Skip to content

Commit b5fa16d

Browse files
committed
⚡ Optimize Tap Loop by hoisting redundant element finding and coordinate calculation.
This change moves the `_element_find` call and the center coordinate calculations outside the repetition loop in the `tap` keyword. This avoids making multiple expensive Appium server calls for the same element when the `count` argument is greater than 1. Key changes: - Hoisted `self._element_find(element, True, True)` outside the loop. - Hoisted `location` and `size` retrieval and `center_x`, `center_y` calculations outside the loop. - The `driver.tap` call remains inside the loop to perform the requested number of taps at the calculated position. Performance impact: - Number of `_element_find` calls reduced from `count` to 1. - Significant reduction in network/IPC latency for multi-tap operations.
1 parent a9e90c0 commit b5fa16d

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

AppiumLibrary/keywords/_touch.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,12 @@ def tap(self, element: Union[str, list], count:int = 1, duration=timedelta(secon
326326
raise ValueError(f"Invalid coordinates format: {element}. Expected a list like [x, y]")
327327

328328
elif isinstance(element, str):
329+
el = self._element_find(element, True, True)
330+
location = el.location
331+
size = el.size
332+
center_x = location['x'] + size['width'] // 2
333+
center_y = location['y'] + size['height'] // 2
329334
for _ in range(count):
330-
el = self._element_find(element, True, True)
331-
location = el.location
332-
size = el.size
333-
center_x = location['x'] + size['width'] // 2
334-
center_y = location['y'] + size['height'] // 2
335335
driver.tap([(center_x, center_y)], duration.total_seconds() * 1000)
336336

337337
else:

0 commit comments

Comments
 (0)