11import time
22from typing import Tuple , Literal
33
4+ from selenium .webdriver import ActionChains
5+
46from screens .element_interactor import ElementInteractor
7+ from utils .logger import log
58
69
710Locator = Tuple [str , str ]
@@ -14,6 +17,7 @@ def __init__(self, driver):
1417 super ().__init__ (driver )
1518
1619 def click (self , locator : Locator , condition : Condition = "clickable" ):
20+ """Click on element"""
1721 element = self .element (locator , condition = condition )
1822 element .click ()
1923
@@ -36,27 +40,61 @@ def tap(self, locator: Locator, duration: float = 500, **kwargs):
3640
3741 def swipe (
3842 self ,
39- relative_start_x : float ,
40- relative_start_y : float ,
41- relative_end_x : float ,
42- relative_end_y : float ,
43+ start_ratio : Tuple [float , float ],
44+ end_ratio : Tuple [float , float ],
4345 duration_ms : int = 200 ,
4446 ) -> None :
47+ """Performs a swipe gesture based on screen size ratios.
48+
49+ :param start_ratio: (x, y) tuple for the starting position (0-1 range)
50+ :param end_ratio: (x, y) tuple for the ending position (0-1 range)
51+ :param duration_ms: Swipe duration in milliseconds (default: 200ms)
52+ Usage:
53+ Swipe left self.swipe((0.9, 0.5), (0.1, 0.5))
54+
55+ """
4556 size = self .get_screen_size ()
46- width = size ["width" ]
47- height = size ["height" ]
48- start_x = int (width * relative_start_x )
49- start_y = int (height * relative_start_y )
50- end_x = int (width * relative_end_x )
51- end_y = int (height * relative_end_y )
52- self .driver .swipe (
53- start_x = start_x ,
54- start_y = start_y ,
55- end_x = end_x ,
56- end_y = end_y ,
57- duration_ms = duration_ms ,
57+ start_x , start_y = (
58+ int (size ["width" ] * start_ratio [0 ]),
59+ int (size ["height" ] * start_ratio [1 ]),
60+ )
61+ end_x , end_y = (
62+ int (size ["width" ] * end_ratio [0 ]),
63+ int (size ["height" ] * end_ratio [1 ]),
5864 )
5965
66+ self .driver .swipe (start_x , start_y , end_x , end_y , duration = duration_ms )
67+
68+ def swipe_to_delete (
69+ self ,
70+ locator : Locator ,
71+ direction : Literal ["left" , "right" ],
72+ duration_ms : int = 500 ,
73+ start_ratio : float = 0.8 ,
74+ end_ratio : float = 0.2 ,
75+ ):
76+ """Swipes an element left or right to trigger a delete action.
77+
78+ :param locator: The locator of the element to swipe.
79+ :param direction: "left" or "right" to define the swipe direction.
80+ :param duration_ms: Duration of the swipe in milliseconds.
81+ :param start_ratio: Start position as a percentage of element width.
82+ :param end_ratio: End position as a percentage of element width.
83+ """
84+ element = self .element (locator )
85+ location = element .location
86+ size = element .size
87+
88+ start_x = location ["x" ] + size ["width" ] * (
89+ start_ratio if direction == "left" else (1 - start_ratio )
90+ )
91+ end_x = location ["x" ] + size ["width" ] * (
92+ end_ratio if direction == "left" else (1 - end_ratio )
93+ )
94+ start_y = location ["y" ] + size ["height" ] // 2
95+
96+ self .driver .swipe (start_x , start_y , end_x , start_y , duration_ms )
97+
6098 def scroll (
6199 self ,
62100 directions : Direction = "down" ,
@@ -121,21 +159,18 @@ def scroll_until_element_visible(
121159
122160 def type (self , locator : Locator , text : str ):
123161 element = self .element (locator )
162+ element .clear ()
124163 element .send_keys (text )
125164
126165 def double_tap (
127166 self , locator : Locator , condition : Condition = "clickable" , ** kwargs
128167 ):
129168 """Double taps on an element."""
130169 try :
131- element = self .element (locator , condition = condition , ** kwargs )
132- # TODO
170+ self .double_tap_actions (locator , condition = condition , ** kwargs )
133171 except Exception as e :
134172 print (f"Error during double tap action: { e } " )
135173
136- def long_press (self ):
137- pass
138-
139174 @staticmethod
140175 def sleep (kwargs ):
141176 try :
0 commit comments