11# -*- coding: utf-8 -*-
22'''
3- Copyright (C) 2012-2018 Diego Torres Milano
3+ Copyright (C) 2012-2019 Diego Torres Milano
44Created on Feb 2, 2015
55
66Licensed under the Apache License, Version 2.0 (the "License");
3030import sys
3131import threading
3232
33- try :
34- import requests
35-
36- REQUESTS_AVAILABLE = True
37- except :
38- REQUESTS_AVAILABLE = False
3933import time
4034from com .dtmilano .android .adb .adbclient import AdbClient
4135from com .dtmilano .android .common import obtainAdbPath
36+ import culebratester_client
4237
4338__author__ = 'diego'
4439
@@ -96,27 +91,15 @@ class UiAutomatorHelper:
9691 TEST_RUNNER = 'com.dtmilano.android.uiautomatorhelper.UiAutomatorHelperTestRunner'
9792
9893 def __init__ (self , adbclient , adb = None , localport = 9999 , remoteport = 9999 , hostname = 'localhost' ):
99- if not REQUESTS_AVAILABLE :
100- raise Exception ('''Python Requests is needed for UiAutomatorHelper to work.
101-
102- On Ubuntu install
103-
104- $ sudo apt-get install python-requests
105-
106- On OSX install
107-
108- $ easy_install requests
109- ''' )
110-
11194 self .adbClient = adbclient
11295 ''' The adb client (a.k.a. device) '''
113- instrumentation = self .adbClient .shell ('pm list instrumentation %s' % self .PACKAGE )
114- if not instrumentation :
115- raise RuntimeError ('The target device does not contain the instrumentation for %s' % self .PACKAGE )
116- if not re .match ('instrumentation:%s/%s \(target=%s\)' % (self .TEST_CLASS , self .TEST_RUNNER , self .PACKAGE ),
117- instrumentation ):
118- raise RuntimeError ('The instrumentation found for %s does not match the expected %s/%s' % (
119- self .PACKAGE , self .TEST_CLASS , self .TEST_RUNNER ))
96+ # instrumentation = self.adbClient.shell('pm list instrumentation %s' % self.PACKAGE)
97+ # if not instrumentation:
98+ # raise RuntimeError('The target device does not contain the instrumentation for %s' % self.PACKAGE)
99+ # if not re.match('instrumentation:%s/%s \(target=%s\)' % (self.TEST_CLASS, self.TEST_RUNNER, self.PACKAGE),
100+ # instrumentation):
101+ # raise RuntimeError('The instrumentation found for %s does not match the expected %s/%s' % (
102+ # self.PACKAGE, self.TEST_CLASS, self.TEST_RUNNER))
120103 self .adb = self .__whichAdb (adb )
121104 ''' The adb command '''
122105 self .osName = platform .system ()
@@ -125,15 +108,17 @@ def __init__(self, adbclient, adb=None, localport=9999, remoteport=9999, hostnam
125108 ''' Is it Mac OSX? '''
126109 self .hostname = hostname
127110 ''' The hostname we are connecting to. '''
128- if hostname in ['localhost' , '127.0.0.1' ]:
129- self .__redirectPort (localport , remoteport )
130- self .__runTests ()
131- self .baseUrl = 'http://%s:%d' % (hostname , localport )
132- try :
133- self .session = self .__connectSession ()
134- except RuntimeError as ex :
135- self .thread .forceStop ()
136- raise ex
111+ #if hostname in ['localhost', '127.0.0.1']:
112+ # self.__redirectPort(localport, remoteport)
113+ #self.__runTests()
114+ #self.baseUrl = 'http://%s:%d' % (hostname, localport)
115+ #try:
116+ # self.session = self.__connectSession()
117+ #except RuntimeError as ex:
118+ # self.thread.forceStop()
119+ # raise ex
120+ print ('⚠️ CulebraTester2 server should have been started and port redirected.' , file = sys .stderr )
121+ self .api_instance = culebratester_client .DefaultApi (culebratester_client .ApiClient ())
137122
138123 def __connectSession (self ):
139124 if DEBUG :
@@ -195,16 +180,7 @@ def __runTests(self):
195180 print ("__runTests: end" , file = sys .stderr )
196181
197182 def __httpCommand (self , url , params = None , method = 'GET' ):
198- if method == 'GET' :
199- if params :
200- response = self .session .get (self .baseUrl + url , params = params )
201- else :
202- response = self .session .get (self .baseUrl + url )
203- elif method == 'PUT' :
204- response = self .session .put (self .baseUrl + url , params = params )
205- else :
206- raise RuntimeError ("method not supported: " + method )
207- return response .content
183+ raise RuntimeError ("this method should not be used" )
208184
209185 #
210186 # Device
@@ -216,11 +192,12 @@ def getDisplayRealSize(self):
216192 # UiAutomatorHelper internal commands
217193 #
218194 def quit (self ):
219- try :
220- self .__httpCommand ('/UiAutomatorHelper/quit' )
221- except :
222- pass
223- self .session .close ()
195+ # try:
196+ # self.__httpCommand('/UiAutomatorHelper/quit')
197+ # except:
198+ # pass
199+ # self.session.close()
200+ pass
224201
225202 #
226203 # UiDevice
@@ -235,31 +212,10 @@ def click(self, **kwargs):
235212 return self .__httpCommand ('/UiDevice/click' , params )
236213
237214 def dumpWindowHierarchy (self ):
238- dump = self .__httpCommand ('/UiDevice/dumpWindowHierarchy' ).decode (encoding = 'UTF-8' , errors = 'replace' )
239- if DEBUG :
240- print ("DUMP: " , dump , file = sys .stderr )
241- return dump
215+ return self .api_instance .ui_device_dump_window_hierarchy_get (format = 'JSON' )
242216
243217 def findObject (self , ** kwargs ):
244- params = kwargs
245- if not ('resourceId' in params or 'bySelector' in params ):
246- raise RuntimeError ('findObject: resourceId or bySelector must have a value' )
247- response = self .__httpCommand ('/UiDevice/findObject' , params )
248- # { "status": "OK", "oid": 1, "className": "android.view.View"}
249- if DEBUG :
250- print ("UiAutomatorHelper: findObject: response=" , response , file = sys .stderr )
251- r = json .loads (response )
252- if r ['status' ] == 'OK' :
253- if DEBUG :
254- print ("UiAutomatorHelper: findObject: returning" , int (r ['oid' ]), file = sys .stderr )
255- return UiObject2 (self , int (r ['oid' ]))
256- elif r ['status' ] == 'ERROR' :
257- if DEBUG :
258- print ("UiAutomatorHelper: findObject: returning" , int (r ['oid' ]), file = sys .stderr )
259- if r ['statusCode' ] == - 1 :
260- # Object not found
261- return None
262- raise RuntimeError ("Error: " + response )
218+ return self .api_instance .ui_device_find_object_get (** kwargs )
263219
264220 def longClick (self , ** kwargs ):
265221 params = kwargs
@@ -277,14 +233,13 @@ def openQuickSettings(self):
277233 return self .__httpCommand ('/UiDevice/openQuickSettings' )
278234
279235 def pressBack (self ):
280- return self .__httpCommand ( '/UiDevice/pressBack' )
236+ return self .api_instance . ui_device_press_back_get ( )
281237
282238 def pressHome (self ):
283- return self .__httpCommand ( '/UiDevice/pressHome' )
239+ return self .api_instance . ui_device_press_home_get ( )
284240
285241 def pressKeyCode (self , keyCode , metaState = 0 ):
286- params = {'keyCode' : keyCode , 'metaState' : metaState }
287- return self .__httpCommand ('/UiDevice/pressKeyCode' , params )
242+ return self .api_instance .ui_device_press_key_code_get (key_code = keyCode , meta_state = metaState )
288243
289244 def pressRecentApps (self ):
290245 return self .__httpCommand ('/UiDevice/pressRecentApps' )
@@ -300,8 +255,7 @@ def swipe(self, startX=-1, startY=-1, endX=-1, endY=-1, steps=10, segments=[], s
300255 return self .__httpCommand ('/UiDevice/swipe' , params )
301256
302257 def takeScreenshot (self , scale = 1.0 , quality = 90 ):
303- params = {'scale' : scale , 'quality' : quality }
304- return self .__httpCommand ('/UiDevice/takeScreenshot' , params )
258+ return self .api_instance .ui_device_screenshot_get (scale = scale , quality = quality )
305259
306260 def waitForIdle (self , timeout ):
307261 params = {'timeout' : timeout }
0 commit comments