1818from selenium .common .exceptions import NoSuchElementException
1919
2020from appium import webdriver
21+ from appium .webdriver .common .mobileby import MobileBy
2122from appium .webdriver .common .touch_action import TouchAction
2223import desired_capabilities
24+ from helper .test_helper import wait_for_element
2325
2426# the emulator is sometimes slow
25- SLEEPY_TIME = 2
27+ SLEEPY_TIME = 3
2628
2729
2830class TouchActionTests (unittest .TestCase ):
@@ -37,28 +39,26 @@ def test_tap(self):
3739 el = self .driver .find_element_by_accessibility_id ('Animation' )
3840 action = TouchAction (self .driver )
3941 action .tap (el ).perform ()
40- el = self .driver . find_element_by_accessibility_id ( 'Bouncing Balls' )
42+ el = wait_for_element ( self .driver , MobileBy . ACCESSIBILITY_ID , 'Bouncing Balls' , SLEEPY_TIME )
4143 self .assertIsNotNone (el )
4244
4345 def test_tap_x_y (self ):
4446 el = self .driver .find_element_by_accessibility_id ('Animation' )
4547 action = TouchAction (self .driver )
4648 action .tap (el , 100 , 10 ).perform ()
4749
48- sleep (SLEEPY_TIME )
49- el = self .driver .find_element_by_accessibility_id ('Bouncing Balls' )
50+ el = wait_for_element (self .driver , MobileBy .ACCESSIBILITY_ID , 'Bouncing Balls' , SLEEPY_TIME )
5051 self .assertIsNotNone (el )
5152
5253 def test_tap_twice (self ):
53- el = self .driver .find_element_by_name ('Text' )
54+ el = self .driver .find_element_by_accessibility_id ('Text' )
5455 action = TouchAction (self .driver )
5556 action .tap (el ).perform ()
56- sleep (SLEEPY_TIME )
5757
58- el = self .driver . find_element_by_name ( 'LogTextBox' )
58+ el = wait_for_element ( self .driver , MobileBy . ACCESSIBILITY_ID , 'LogTextBox' , SLEEPY_TIME )
5959 action .tap (el ).perform ()
6060
61- el = self .driver . find_element_by_name ( 'Add' )
61+ el = wait_for_element ( self .driver , MobileBy . ACCESSIBILITY_ID , 'Add' , SLEEPY_TIME )
6262 action .tap (el , count = 2 ).perform ()
6363
6464 els = self .driver .find_elements_by_class_name ('android.widget.TextView' )
@@ -69,49 +69,40 @@ def test_press_and_immediately_release(self):
6969 action = TouchAction (self .driver )
7070 action .press (el ).release ().perform ()
7171
72- sleep (SLEEPY_TIME )
73- el = self .driver .find_element_by_accessibility_id ('Bouncing Balls' )
72+ el = wait_for_element (self .driver , MobileBy .ACCESSIBILITY_ID , 'Bouncing Balls' , SLEEPY_TIME )
7473 self .assertIsNotNone (el )
7574
7675 def test_press_and_immediately_release_x_y (self ):
7776 el = self .driver .find_element_by_accessibility_id ('Animation' )
7877 action = TouchAction (self .driver )
7978 action .press (el , 100 , 10 ).release ().perform ()
8079
81- sleep (SLEEPY_TIME )
82- el = self .driver .find_element_by_accessibility_id ('Bouncing Balls' )
80+ el = wait_for_element (self .driver , MobileBy .ACCESSIBILITY_ID , 'Bouncing Balls' , SLEEPY_TIME )
8381 self .assertIsNotNone (el )
8482
8583 def test_press_and_wait (self ):
86- el1 = self .driver .find_element_by_name ('Content' )
84+ el1 = self .driver .find_element_by_accessibility_id ('Content' )
8785 el2 = self .driver .find_element_by_accessibility_id ('Animation' )
8886
8987 action = TouchAction (self .driver )
9088 action .press (el1 ).move_to (el2 ).perform ()
9189
92- sleep (SLEEPY_TIME )
93- el = self .driver .find_element_by_accessibility_id ('Views' )
94- # self.assertIsNotNone(el)
90+ el = wait_for_element (self .driver , MobileBy .ACCESSIBILITY_ID , 'Views' , SLEEPY_TIME )
9591 action .tap (el ).perform ()
9692
97- sleep (SLEEPY_TIME )
98- el = self .driver .find_element_by_accessibility_id ('Expandable Lists' )
99- # self.assertIsNotNone(el)
93+ el = wait_for_element (self .driver , MobileBy .ACCESSIBILITY_ID , 'Expandable Lists' , SLEEPY_TIME )
10094 action .tap (el ).perform ()
10195
102- sleep (SLEEPY_TIME )
103- el = self .driver .find_element_by_accessibility_id ('1. Custom Adapter' )
104- # self.assertIsNotNone(el)
96+ el = wait_for_element (self .driver , MobileBy .ACCESSIBILITY_ID , '1. Custom Adapter' , SLEEPY_TIME )
10597 action .tap (el ).perform ()
10698
107- sleep (SLEEPY_TIME )
108- el = self .driver .find_element_by_name ('People Names' )
109- # self.assertIsNotNone(el)
99+ el = wait_for_element (self .driver , MobileBy .ANDROID_UIAUTOMATOR ,
100+ 'new UiSelector().text("People Names")' , SLEEPY_TIME )
110101 action .press (el ).wait (2000 ).perform ()
111102
112- sleep (SLEEPY_TIME )
113103 # 'Sample menu' only comes up with a long press, not a press
114- el = self .driver .find_element_by_name ('Sample menu' )
104+ el = wait_for_element (self .driver , MobileBy .ANDROID_UIAUTOMATOR ,
105+ 'new UiSelector().text("Sample menu")' , SLEEPY_TIME )
115106 self .assertIsNotNone (el )
116107
117108 def test_press_and_moveto (self ):
@@ -135,104 +126,105 @@ def test_press_and_moveto_x_y(self):
135126 self .assertIsNotNone (el )
136127
137128 def test_long_press (self ):
138- el1 = self .driver .find_element_by_name ('Content' )
129+ el1 = self .driver .find_element_by_accessibility_id ('Content' )
139130 el2 = self .driver .find_element_by_accessibility_id ('Animation' )
140131
141132 action = TouchAction (self .driver )
142133 action .press (el1 ).move_to (el2 ).perform ()
143134
144135 el = self .driver .find_element_by_accessibility_id ('Views' )
145- # self.assertIsNotNone(el)
146136 action .tap (el ).perform ()
147137
148- el = self .driver .find_element_by_accessibility_id ('Expandable Lists' )
149- # self.assertIsNotNone(el)
138+ el = wait_for_element (self .driver , MobileBy .ACCESSIBILITY_ID , 'Expandable Lists' , SLEEPY_TIME )
150139 action .tap (el ).perform ()
151140
152- el = self .driver .find_element_by_accessibility_id ('1. Custom Adapter' )
153- # self.assertIsNotNone(el)
141+ el = wait_for_element (self .driver , MobileBy .ACCESSIBILITY_ID , '1. Custom Adapter' , SLEEPY_TIME )
154142 action .tap (el ).perform ()
155143
156- el = self .driver . find_element_by_name ( 'People Names' )
157- # self.assertIsNotNone(el )
144+ el = wait_for_element ( self .driver , MobileBy . ANDROID_UIAUTOMATOR ,
145+ 'new UiSelector().text("People Names")' , SLEEPY_TIME )
158146 action .long_press (el ).perform ()
159147
160148 # 'Sample menu' only comes up with a long press, not a tap
161- el = self .driver .find_element_by_name ('Sample menu' )
149+ el = wait_for_element (self .driver , MobileBy .ANDROID_UIAUTOMATOR ,
150+ 'new UiSelector().text("Sample menu")' , SLEEPY_TIME )
162151 self .assertIsNotNone (el )
163152
164153 def test_long_press_x_y (self ):
165- el1 = self .driver .find_element_by_name ('Content' )
154+ el1 = self .driver .find_element_by_accessibility_id ('Content' )
166155 el2 = self .driver .find_element_by_accessibility_id ('Animation' )
167156
168157 action = TouchAction (self .driver )
169158 action .press (el1 ).move_to (el2 ).perform ()
170159
171160 el = self .driver .find_element_by_accessibility_id ('Views' )
172- # self.assertIsNotNone(el)
173161 action .tap (el ).perform ()
174162
175163 el = self .driver .find_element_by_accessibility_id ('Expandable Lists' )
176- # self.assertIsNotNone(el)
177164 action .tap (el ).perform ()
178165
179166 el = self .driver .find_element_by_accessibility_id ('1. Custom Adapter' )
180- # self.assertIsNotNone(el)
181167 action .tap (el ).perform ()
182168
183- # the element "People Names" is located at 0:110 (top left corner)
184- action .long_press (x = 10 , y = 120 ).perform ()
169+ # the element "People Names" is located at 430:310 (top left corner)
170+ # location can be changed by phone resolusion, OS version
171+ action .long_press (x = 430 , y = 310 ).perform ()
185172
186173 # 'Sample menu' only comes up with a long press, not a tap
187- el = self .driver .find_element_by_name ('Sample menu' )
174+ el = wait_for_element (self .driver , MobileBy .ANDROID_UIAUTOMATOR ,
175+ 'new UiSelector().text("Sample menu")' , SLEEPY_TIME )
188176 self .assertIsNotNone (el )
189177
190178 def test_drag_and_drop (self ):
191- el1 = self .driver .find_element_by_name ('Content' )
192- el2 = self .driver .find_element_by_name ('Animation' )
179+ el1 = self .driver .find_element_by_accessibility_id ('Content' )
180+ el2 = self .driver .find_element_by_accessibility_id ('Animation' )
193181 self .driver .scroll (el1 , el2 )
194182
195- el = self .driver .find_element_by_name ('Views' )
183+ el = self .driver .find_element_by_accessibility_id ('Views' )
196184 action = TouchAction (self .driver )
197185 action .tap (el ).perform ()
198186
199- el = self .driver . find_element_by_name ( 'Drag and Drop' )
187+ el = wait_for_element ( self .driver , MobileBy . ACCESSIBILITY_ID , 'Drag and Drop' , SLEEPY_TIME )
200188 action .tap (el ).perform ()
201189
202- dd3 = self .driver . find_element_by_id ( 'com.example.android.apis:id/drag_dot_3' )
190+ dd3 = wait_for_element ( self .driver , MobileBy . ID , 'com.example.android.apis:id/drag_dot_3' , SLEEPY_TIME )
203191 dd2 = self .driver .find_element_by_id ('com.example.android.apis:id/drag_dot_2' )
204192
205193 # dnd is stimulated by longpress-move_to-release
206194 action .long_press (dd3 ).move_to (dd2 ).release ().perform ()
207195
208- el = self .driver .find_element_by_id ('com.example.android.apis:id/drag_result_text ' )
209- self .assertEqual ( 'Dropped!' , el .get_attribute ( ' text' ) )
196+ el = self .driver .find_element_by_id ('com.example.android.apis:id/drag_text ' )
197+ self .assertTrue ( 'drag_dot_3' in el .text )
210198
211199 def test_driver_drag_and_drop (self ):
212- el1 = self .driver .find_element_by_name ('Content' )
213- el2 = self .driver .find_element_by_name ('Animation' )
200+ el1 = self .driver .find_element_by_accessibility_id ('Content' )
201+ el2 = self .driver .find_element_by_accessibility_id ('Animation' )
214202 self .driver .scroll (el1 , el2 )
215203
216- el = self .driver .find_element_by_name ('Views' )
204+ el = self .driver .find_element_by_accessibility_id ('Views' )
217205 action = TouchAction (self .driver )
218206 action .tap (el ).perform ()
219207
220- el = self .driver . find_element_by_name ( 'Drag and Drop' )
208+ el = wait_for_element ( self .driver , MobileBy . ACCESSIBILITY_ID , 'Drag and Drop' , SLEEPY_TIME )
221209 action .tap (el ).perform ()
222210
223211 dd3 = self .driver .find_element_by_id ('com.example.android.apis:id/drag_dot_3' )
224212 dd2 = self .driver .find_element_by_id ('com.example.android.apis:id/drag_dot_2' )
225213
226214 self .driver .drag_and_drop (dd3 , dd2 )
227215
228- el = self .driver .find_element_by_id ('com.example.android.apis:id/drag_result_text ' )
229- self .assertEqual ( 'Dropped!' , el .get_attribute ( ' text' ) )
216+ el = self .driver .find_element_by_id ('com.example.android.apis:id/drag_text ' )
217+ self .assertTrue ( 'drag_dot_3' in el .text )
230218
231219 def test_driver_swipe (self ):
232- self .assertRaises (NoSuchElementException , self .driver .find_element_by_name , 'Views' )
220+ el = self .driver .find_element_by_accessibility_id ('Views' )
221+ action = TouchAction (self .driver )
222+ action .tap (el ).perform ()
223+
224+ self .assertRaises (NoSuchElementException , self .driver .find_element_by_accessibility_id , 'ImageView' )
233225
234- self .driver .swipe (100 , 500 , 100 , 100 , 800 )
235- el = self .driver .find_element_by_name ( 'Views ' )
226+ self .driver .swipe (100 , 1000 , 100 , 100 , 800 )
227+ el = self .driver .find_element_by_accessibility_id ( 'ImageView ' )
236228 self .assertIsNotNone (el )
237229
238230
0 commit comments