2014-12-02 14 views

risposta

7

Indovina che dovrei rispondere alla mia stessa domanda perché ho trovato un modo migliore per farlo. Ho trovato questo progetto che utilizzano uiautomator assieme con un server RPC peso leggero in modo da poter inviare comandi al dispositivo:

https://github.com/xiaocong/android-uiautomator-server#build

Questo rende lo scarico quasi istantaneamente e funziona veramente bello. Egli ha anche un progetto di pitone, se volete vedere come fare le chiamate RPC:

https://github.com/xiaocong/uiautomator

Ma io ho creato un piccolo esempio qui.

Avviare il server:

# Start the process 
process = subprocess.Popen(
     'adb shell uiautomator runtest ' 
     'bundle.jar uiautomator-stub.jar ' 
     '-c com.github.uiautomatorstub.Stub', stdout=subprocess.PIPE, shell=True) 
# Forward adb ports 
subprocess.call('adb forward tcp:9008 tcp:9009') 

funzione di chiamare comandi ("ping", "dumpWindowHierarchy", ecc):

def send_command(self, method_name, *args): 
    """ 
    Send command to the RPC server 

    Args: 
     method_name(string): Name of method to run 
     *args: Arguments 
    """ 
    data = { 
     'jsonrpc': '2.0', 
     'method': method_name, 
     'id': 1, 
    } 
    if args: 
     data['params'] = args 
    request = urllib2.Request(
     'http://localhost:{0}/jsonrpc/0'.format(self.local_port), 
     json.dumps(data), 
     { 
      'Content-type': 'application/json' 
     }) 
    try: 
     result = urllib2.urlopen(request, timeout=30) 
    except Exception as e: 
     return None 
    if result is None: 
     return None 
    json_result = json.loads(result.read()) 
    if 'error' in json_result: 
     raise JsonRPCError('1', 
          'Exception when sending command to ' 
          'UIAutomatorServer: {0}'.format(
           json_result['error'])) 
    return json_result['result'] 

Si noti che si deve spingere i file (bundle.jar anduiautomator -stub.jar) dal primo progetto al dispositivo per primo e metterli in "/ data/local/tmp /"

Problemi correlati