18
18
from selenium .common .exceptions import NoSuchElementException
19
19
20
20
from appium import webdriver
21
+ from appium .webdriver .common .mobileby import MobileBy
21
22
from appium .webdriver .common .touch_action import TouchAction
22
23
import desired_capabilities
24
+ from helper .test_helper import wait_for_element
23
25
24
26
# the emulator is sometimes slow
25
- SLEEPY_TIME = 2
27
+ SLEEPY_TIME = 3
26
28
27
29
28
30
class TouchActionTests (unittest .TestCase ):
@@ -37,28 +39,26 @@ def test_tap(self):
37
39
el = self .driver .find_element_by_accessibility_id ('Animation' )
38
40
action = TouchAction (self .driver )
39
41
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 )
41
43
self .assertIsNotNone (el )
42
44
43
45
def test_tap_x_y (self ):
44
46
el = self .driver .find_element_by_accessibility_id ('Animation' )
45
47
action = TouchAction (self .driver )
46
48
action .tap (el , 100 , 10 ).perform ()
47
49
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 )
50
51
self .assertIsNotNone (el )
51
52
52
53
def test_tap_twice (self ):
53
- el = self .driver .find_element_by_name ('Text' )
54
+ el = self .driver .find_element_by_accessibility_id ('Text' )
54
55
action = TouchAction (self .driver )
55
56
action .tap (el ).perform ()
56
- sleep (SLEEPY_TIME )
57
57
58
- el = self .driver . find_element_by_name ( 'LogTextBox' )
58
+ el = wait_for_element ( self .driver , MobileBy . ACCESSIBILITY_ID , 'LogTextBox' , SLEEPY_TIME )
59
59
action .tap (el ).perform ()
60
60
61
- el = self .driver . find_element_by_name ( 'Add' )
61
+ el = wait_for_element ( self .driver , MobileBy . ACCESSIBILITY_ID , 'Add' , SLEEPY_TIME )
62
62
action .tap (el , count = 2 ).perform ()
63
63
64
64
els = self .driver .find_elements_by_class_name ('android.widget.TextView' )
@@ -69,49 +69,40 @@ def test_press_and_immediately_release(self):
69
69
action = TouchAction (self .driver )
70
70
action .press (el ).release ().perform ()
71
71
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 )
74
73
self .assertIsNotNone (el )
75
74
76
75
def test_press_and_immediately_release_x_y (self ):
77
76
el = self .driver .find_element_by_accessibility_id ('Animation' )
78
77
action = TouchAction (self .driver )
79
78
action .press (el , 100 , 10 ).release ().perform ()
80
79
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 )
83
81
self .assertIsNotNone (el )
84
82
85
83
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' )
87
85
el2 = self .driver .find_element_by_accessibility_id ('Animation' )
88
86
89
87
action = TouchAction (self .driver )
90
88
action .press (el1 ).move_to (el2 ).perform ()
91
89
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 )
95
91
action .tap (el ).perform ()
96
92
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 )
100
94
action .tap (el ).perform ()
101
95
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 )
105
97
action .tap (el ).perform ()
106
98
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 )
110
101
action .press (el ).wait (2000 ).perform ()
111
102
112
- sleep (SLEEPY_TIME )
113
103
# '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 )
115
106
self .assertIsNotNone (el )
116
107
117
108
def test_press_and_moveto (self ):
@@ -135,104 +126,105 @@ def test_press_and_moveto_x_y(self):
135
126
self .assertIsNotNone (el )
136
127
137
128
def test_long_press (self ):
138
- el1 = self .driver .find_element_by_name ('Content' )
129
+ el1 = self .driver .find_element_by_accessibility_id ('Content' )
139
130
el2 = self .driver .find_element_by_accessibility_id ('Animation' )
140
131
141
132
action = TouchAction (self .driver )
142
133
action .press (el1 ).move_to (el2 ).perform ()
143
134
144
135
el = self .driver .find_element_by_accessibility_id ('Views' )
145
- # self.assertIsNotNone(el)
146
136
action .tap (el ).perform ()
147
137
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 )
150
139
action .tap (el ).perform ()
151
140
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 )
154
142
action .tap (el ).perform ()
155
143
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 )
158
146
action .long_press (el ).perform ()
159
147
160
148
# '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 )
162
151
self .assertIsNotNone (el )
163
152
164
153
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' )
166
155
el2 = self .driver .find_element_by_accessibility_id ('Animation' )
167
156
168
157
action = TouchAction (self .driver )
169
158
action .press (el1 ).move_to (el2 ).perform ()
170
159
171
160
el = self .driver .find_element_by_accessibility_id ('Views' )
172
- # self.assertIsNotNone(el)
173
161
action .tap (el ).perform ()
174
162
175
163
el = self .driver .find_element_by_accessibility_id ('Expandable Lists' )
176
- # self.assertIsNotNone(el)
177
164
action .tap (el ).perform ()
178
165
179
166
el = self .driver .find_element_by_accessibility_id ('1. Custom Adapter' )
180
- # self.assertIsNotNone(el)
181
167
action .tap (el ).perform ()
182
168
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 ()
185
172
186
173
# '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 )
188
176
self .assertIsNotNone (el )
189
177
190
178
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' )
193
181
self .driver .scroll (el1 , el2 )
194
182
195
- el = self .driver .find_element_by_name ('Views' )
183
+ el = self .driver .find_element_by_accessibility_id ('Views' )
196
184
action = TouchAction (self .driver )
197
185
action .tap (el ).perform ()
198
186
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 )
200
188
action .tap (el ).perform ()
201
189
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 )
203
191
dd2 = self .driver .find_element_by_id ('com.example.android.apis:id/drag_dot_2' )
204
192
205
193
# dnd is stimulated by longpress-move_to-release
206
194
action .long_press (dd3 ).move_to (dd2 ).release ().perform ()
207
195
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 )
210
198
211
199
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' )
214
202
self .driver .scroll (el1 , el2 )
215
203
216
- el = self .driver .find_element_by_name ('Views' )
204
+ el = self .driver .find_element_by_accessibility_id ('Views' )
217
205
action = TouchAction (self .driver )
218
206
action .tap (el ).perform ()
219
207
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 )
221
209
action .tap (el ).perform ()
222
210
223
211
dd3 = self .driver .find_element_by_id ('com.example.android.apis:id/drag_dot_3' )
224
212
dd2 = self .driver .find_element_by_id ('com.example.android.apis:id/drag_dot_2' )
225
213
226
214
self .driver .drag_and_drop (dd3 , dd2 )
227
215
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 )
230
218
231
219
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' )
233
225
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 ' )
236
228
self .assertIsNotNone (el )
237
229
238
230
0 commit comments