From b5fa16de1eb6290ec4707f19e2c9bd6c45a02c49 Mon Sep 17 00:00:00 2001 From: Dor-bl <59066376+Dor-bl@users.noreply.github.com> Date: Sun, 8 Mar 2026 20:46:29 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20Tap=20Loop=20by=20hoisti?= =?UTF-8?q?ng=20redundant=20element=20finding=20and=20coordinate=20calcula?= =?UTF-8?q?tion.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- AppiumLibrary/keywords/_touch.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/AppiumLibrary/keywords/_touch.py b/AppiumLibrary/keywords/_touch.py index 3c744dc..ba3ed02 100644 --- a/AppiumLibrary/keywords/_touch.py +++ b/AppiumLibrary/keywords/_touch.py @@ -326,12 +326,12 @@ def tap(self, element: Union[str, list], count:int = 1, duration=timedelta(secon raise ValueError(f"Invalid coordinates format: {element}. Expected a list like [x, y]") elif isinstance(element, str): + el = self._element_find(element, True, True) + location = el.location + size = el.size + center_x = location['x'] + size['width'] // 2 + center_y = location['y'] + size['height'] // 2 for _ in range(count): - el = self._element_find(element, True, True) - location = el.location - size = el.size - center_x = location['x'] + size['width'] // 2 - center_y = location['y'] + size['height'] // 2 driver.tap([(center_x, center_y)], duration.total_seconds() * 1000) else: