def jisuanji(): # 滑动解锁 jiesuo() # 打开计算器 device.startActivity(component="com.tencent.qrom.calculator/com.tencent.qrom.calculator.Calculator") time.sleep(1) print ("ok") # 将device对象包装成EasyMonkeyDevice类对象 ed = EasyMonkeyDevice(device) print ("okok") # 96*3= ed.touch(By.id("id/digit9"), MonkeyDevice.DOWN_AND_UP) ed.touch(By.id("id/digit6"), MonkeyDevice.DOWN_AND_UP) ed.touch(By.id("id/mul"), MonkeyDevice.DOWN_AND_UP) ed.touch(By.id("id/digit3"), MonkeyDevice.DOWN_AND_UP) ed.touch(By.id("id/equal"), MonkeyDevice.DOWN_AND_UP) print ("ok2") # 通过ID获取mText h = ed.getText(By.id("id/digit9")) print (h)
else: print >> sys.stderr, "'hello' not found" else: print >> sys.stderr, "'Show Dialog' button not found" else: # MonkeyRunner from com.android.monkeyrunner.easy import EasyMonkeyDevice from com.android.monkeyrunner.easy import By easyDevice = EasyMonkeyDevice(device) showDialogButton = By.id('id/show_dialog_button') if showDialogButton: easyDevice.touch(showDialogButton, MonkeyDevice.DOWN_AND_UP) ViewClient.sleep(3) editText = By.id('id/0x123456') print editText easyDevice.type(editText, 'Donald') ViewClient.sleep(3) ok = By.id('id/button1') if ok: # 09-08 20:16:41.119: D/MonkeyStub(1992): translateCommand: tap 348 268 easyDevice.touch(ok, MonkeyDevice.DOWN_AND_UP) hello = By.id('id/hello') if hello: if easyDevice.getText(hello) == "Hello Donald": print "OK" else: print "FAIL" else: print >> sys.stderr, "'hello' not found"
easy_device = EasyMonkeyDevice(device) print('Tap 8') easy_device.touch(By.id('id/digit_8'), MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(1.0) print('Tap *') easy_device.touch(By.id('id/op_mul'), MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(1.0) print('Tap 9') easy_device.touch(By.id('id/digit_9'), MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(1.0) #print('Tap =') #easy_device.touch(By.id('id/eq'),MonkeyDevice.DOWN_AND_UP) if easy_device.exists(By.id('id/result')) == True: text_result = easy_device.getText(By.id('id/result')) if text_result.encode('utf-8') == "72": print("乘法运算正确") else: print("乘法运算错误") else: print("没有id/result") print('清除') easy_device.touch(By.id('id/digit_8'), MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(1.0) ''' text_formula = easy_device.getText(By.id('id/formula')) print(text_formula.encode('utf-8')) print(text_result.encode('utf-8'))
class wrapEasyMonkey: ''' ############################# Wrap easy monkey class ############################# ''' def __init__(self, deviceId): device = MonkeyRunner.waitForConnection(10, deviceId) self.debug( "__int__: creating the wrap easy monkey object with deviceid %s" % deviceId) self.deviceId = deviceId self.easyDevice = EasyMonkeyDevice(device) self.device = device #self.DOWN = TouchPressType.DOWN.getIdentifier() #self.UP = TouchPressType.UP.getIdentifier() #self.DOWN_AND_UP = TouchPressType.DOWN_AND_UP.getIdentifier() self.DOWN = self.device.DOWN self.UP = self.device.UP self.DOWN_AND_UP = self.device.DOWN_AND_UP self.caseManager = testCaseManager(self) self.debug('created the wrapEasyDevice') def waitForConnection(self, seconds): try: return MonkeyRunner.waitForConnection(seconds) except: self.error("waitForConnection error") sys.exc_info() traceback.print_exc() return None def startActivity(self, activity): try: self.debug("starting the activity... %s" % activity) self.device.startActivity(component=activity) except: self.error("starting the activity %s error" % activity) sys.exc_info() traceback.print_exc() return False ''' type charactors, special charactors please the press function to input the keycode ''' def type(self, content): self.debug('device input the %s' % content) self.device.type(content) ''' press keycode, special charactor and direction button ... ''' def press(self, keycode, type): self.debug('device press the key "%s" ' % keycode) #self.sleep(0.2) self.device.press(keycode, type) ''' sleep function ''' def sleep(self, seconds): self.debug('sleeping %f seconds' % seconds) MonkeyRunner.sleep(seconds) ''' wrap easyMonkeyDevice by function will return the view object if found.if do not found will return None. ''' def getView(self, id): self.debug('calling getview function by the id (%s)' % id) for tmp in range(repeatTimesOnError): try: return By.id(id) except: self.debug( 'getView: the %dst time error by id (%s) , will retry ' % (tmp, id)) MonkeyRunner.sleep(1) continue self.error( 'getView: sorry , still can\'t get the view by this id (%s). please check the view ' % id) sys.exc_info() traceback.print_exc() return None ''' if the id object is a textview, then will clear all the text ''' def clearTextById(self, id): self.debug('calling clearTextById function by the id (%s)' % id) if (self.checkIdExist(id)): if not self.isFocused(id): self.touchViewById(id, self.DOWN_AND_UP) TextView = self.getView(id) rangenumber = len(self.getText(TextView)) for x in range(rangenumber): self.device.press('KEYCODE_DEL', self.DOWN_AND_UP) for x in range(rangenumber): self.device.press('KEYCODE_FORWARD_DEL', self.DOWN_AND_UP) self.debug('clearTextById: cleared the text in id (%s)' % id) return True self.error('clearTextById: sorry ,the id (%s) is not exist ' % id) sys.exc_info() traceback.print_exc() return False ''' wrap get text function. return text string content of the view object. If can not found the view object , will return None. ''' def getText(self, view): self.debug('calling getText function') for tmp in range(repeatTimesOnError): try: return self.easyDevice.getText(view).encode( sys.getdefaultencoding()) except: self.debug( 'getText: the %dst time getText error , will retry ' % tmp) MonkeyRunner.sleep(1) continue self.error( 'getText: sorry , still can\'t get the text. please check the view is exist or not , or does the view have text property?' ) sys.exc_info() traceback.print_exc() return None ''' ''' def getTextById(self, id): self.debug('calling getTextById function') for tmp in range(repeatTimesOnError): try: return self.getText(self.getView(id)) except: self.debug( 'getTextById: the %dst time getTextById error id (%s) , will retry ' % (tmp, id)) MonkeyRunner.sleep(1) continue self.error( 'getTextById: sorry , still can\'t get the text by id "%s". please check the view is exist or not , or does the view have text property?' % id) sys.exc_info() traceback.print_exc() return None ''' wrap easyMonkeyDevice touch view function return true or false (if cannot locate the view ,will return false) ''' def touchView(self, view, type): self.debug('calling touchView function') for tmp in range(repeatTimesOnError): try: self.easyDevice.touch(view, type) return True except: self.debug( 'touchView: the %dst time touch error , not found the view , will retry ' % tmp) if (tmp > 1 & DEBUG): self.debug('Please wait to touch the view') MonkeyRunner.sleep(1) continue self.error( 'touchView: sorry , still can\'t touch view. please check the view is exist or not , or increase the repeat times variable?' ) sys.exc_info() traceback.print_exc() return False def touchViewById(self, id, type): self.debug('calling touchViewById function') for tmp in range(repeatTimesOnError): try: self.easyDevice.touch(By.id(id), type) return True except: self.debug( 'touchViewById: the %dst time touch error by this id (%s) , not found the view , will retry ' % (tmp, id)) if (tmp > 1 & DEBUG): self.debug('Please wait to touch the view') MonkeyRunner.sleep(1) continue self.error( 'touchViewById: sorry , still can\'t touch view. please check the view is exist or not , or increase the repeat times variable?' ) sys.exc_info() traceback.print_exc() return False ''' wrap touch point function , touch screen position return true or false always return true actually ''' def touchPoint(self, x, y, type): self.debug('calling touch the point ') for tmp in range(repeatTimesOnError): try: self.device.touch(x, y, type) return True except: self.debug( 'touchPoint: %d time touch point error , will retry ' % tmp) MonkeyRunner.sleep(1) continue self.error( 'touchPoint: sorry , still can\'t touch point. please check the view is exist or not , or increase the repeat times variable?' ) sys.exc_info() traceback.print_exc() return False ''' has the view is focused or not ''' def isFocused(self, id): self.debug('checking the view is focused or not') #hierarchyViewer = self.device.getHierarchyViewer() #print hierarchyViewer.findViewById(id).hasFocus for tmp in range(repeatTimesOnError): try: hierarchyViewer = self.device.getHierarchyViewer() return hierarchyViewer.findViewById(id).hasFocus except: self.debug( 'isFocused: the %dst time check focus error , will retry ' % tmp) MonkeyRunner.sleep(1) continue self.error('isFocused: error occured') sys.exc_info() traceback.print_exc() return False ''' ''' def isExist(self, id): #self.debug('check the id is exist or not') for tmp in range(repeatTimesOnError): try: if (self.easyDevice.exists(self.getView(id))): return True else: self.debug( 'isExist: %s this id does not exists,will try check again' % id) MonkeyRunner.sleep(1) continue except: self.debug( 'isExist: the %dst time check id (%s) existing error , , will retry ' % (tmp, id)) MonkeyRunner.sleep(1) continue self.error('isExist: error occured') sys.exc_info() traceback.print_exc() return False def checkIdExist(self, id): self.debug('checking the id (%s) exist or not' % id) for tmp in range(repeatTimesOnError): try: if (self.easyDevice.exists(self.getView(id))): return True else: self.debug( 'checkIdExist: %s this id does not exists,will try check again' % id) MonkeyRunner.sleep(1) continue except: self.debug( 'checkIdExist: the %dst time check id (%s) existing error , , will retry ' % (tmp, id)) MonkeyRunner.sleep(1) continue self.error('checkIdExist: error occured') sys.exc_info() traceback.print_exc() return False def getPosition(self, id): self.debug('check the view is focused or not') for tmp in range(repeatTimesOnError): try: hierarchyViewer = self.device.getHierarchyViewer() print hierarchyViewer.findViewById(id).left print hierarchyViewer.findViewById(id).top print hierarchyViewer.findViewById(id).width print hierarchyViewer.findViewById(id).height return hierarchyViewer.findViewById(id).left except: MonkeyRunner.sleep(1) continue self.error('getPosition: error occured') sys.exc_info() traceback.print_exc() return None def touchDialogById(self, id, type): #self.debug('touch the dialog button , here need the parent id') hierarchyViewer = self.device.getHierarchyViewer() width = self.device.getProperty("display.width") height = self.device.getProperty("display.height") x = hierarchyViewer.findViewById(id).left y = hierarchyViewer.findViewById(id).top ''' print hierarchyViewer.findViewById(id).scrollX print hierarchyViewer.findViewById(id).scrollY print hierarchyViewer.findViewById(id).marginTop print hierarchyViewer.findViewById(id).marginLeft print hierarchyViewer.findViewById(id).marginRight print hierarchyViewer.findViewById(id).marginBottom print hierarchyViewer.findViewById(id).left print hierarchyViewer.findViewById(id).top print hierarchyViewer.findViewById(id).width print hierarchyViewer.findViewById(id).height print 'margin' print width print height print hierarchyViewer.findViewById(id).properties print '------------------' ''' p = hierarchyViewer.findViewById(id).parent.parent #print p.properties myself = hierarchyViewer.findViewById(id) content = hierarchyViewer.findViewById('id/content') x += p.left + (int(width) - content.width) / 2 + myself.width / 2 y += p.top + (int(height) - content.height) / 2 + myself.height / 2 #( int(width) - hierarchyViewer.findViewById(id).width)/2 + hierarchyViewer.findViewById(id).left + hierarchyViewer.findViewById(id).width/2 #y += ( int(height) - hierarchyViewer.findViewById(id).height)/2 + hierarchyViewer.findViewById(id).top + hierarchyViewer.findViewById(id).height/2 #print x #print y self.touchPoint(x, y, type) def touchDialog(self, parentIdPosition, id, type): self.debug('touch the dialog button , here need the parent id') hierarchyViewer = self.device.getHierarchyViewer() #print hierarchyViewer.findViewById(parentId).left #print hierarchyViewer.findViewById(parentId).top x = hierarchyViewer.findViewById( parentId).left + (hierarchyViewer.findViewById(parentId).width - hierarchyViewer.findViewById(id).width) / 2 y = hierarchyViewer.findViewById( parentId).top + (hierarchyViewer.findViewById(parentId).height - hierarchyViewer.findViewById(id).height) / 2 print x print y self.touchPoint(x, y, type) def touchDialogButton(self, type): if DEBUG: self.debug( 'touch the dialog button , thru controling the direction key') #self.device.press('KEYCODE_DPAD_DOWN',MonkeyDevice.DOWN_AND_UP) if type == 1: self.press('KEYCODE_DPAD_DOWN', MonkeyDevice.DOWN_AND_UP) #self.press('KEYCODE_DPAD_DOWN',MonkeyDevice.DOWN_AND_UP) self.press('KEYCODE_ENTER', MonkeyDevice.DOWN_AND_UP) if type == 2: #self.press('KEYCODE_DPAD_DOWN',MonkeyDevice.DOWN_AND_UP) self.press('KEYCODE_DPAD_DOWN', MonkeyDevice.DOWN_AND_UP) self.press('KEYCODE_DPAD_RIGHT', self.DOWN_AND_UP) self.press('KEYCODE_ENTER', self.DOWN_AND_UP) if type == 0: self.press('KEYCODE_ENTER', self.DOWN_AND_UP) def touchContextMenu(self, position): if DEBUG: self.debug('touch the context menu') self.press('KEYCODE_MENU', self.DOWN_AND_UP) for tmp in range(position + 1): MonkeyRunner.sleep(0.5) self.press('KEYCODE_DPAD_RIGHT', self.DOWN_AND_UP) self.press('KEYCODE_ENTER', self.DOWN_AND_UP) ''' def touchDialogButtonRight(self,type): self.device.press('KEYCODE_DPAD_DOWN',type) self.device.press('KEYCODE_DPAD_DOWN',type) self.device.press('KEYCODE_DPAD_RIGHT',type) self.device.press('KEYCODE_ENTER',type) ''' def debug(self, debuginfo): if DEBUG: print '[%s] DEBUG: %s ' % (datetime.today(), debuginfo) def info(self, info): if INFO: print '[%s] Info: %s ' % (datetime.today(), info) def error(self, error): if ERROR: print '[%s] ERROR: %s ' % (datetime.today(), error) def takeSnapshot(self): print '----------start take snapshot-------------' for tmp in range(5): try: snapshot = self.device.takeSnapshot() print '----------end take snapshot-%s------------' % datetime.today( ) return snapshot except: continue self.error('takeSnapshot: error occured') sys.exc_info() traceback.print_exc() return False
#! /usr/bin/env python ''' Copyright (C) 2012 Diego Torres Milano Created on Sep 8, 2012 @author: diego @see: http://code.google.com/p/android/issues/detail?id=36544 ''' import re import sys import os # This must be imported before MonkeyRunner and MonkeyDevice, # otherwise the import fails. # PyDev sets PYTHONPATH, use it try: for p in os.environ['PYTHONPATH'].split(':'): if not p in sys.path: sys.path.append(p) except: pass try: sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src')) except: pass from androidviewclient3.viewclient import ViewClient device, serialno = ViewClient.connectToDeviceOrExit() FLAG_ACTIVITY_NEW_TASK = 0x10000000 # We are not using Settings as the bug describes because there's no WiFi dialog in emulator #componentName = 'com.android.settings/.Settings' componentName = 'com.dtmilano.android.sampleui/.MainActivity'
print "FAIL" else: print >> sys.stderr, "'hello' not found" else: print >> sys.stderr, "'Show Dialog' button not found" else: # MonkeyRunner from com.android.monkeyrunner.easy import EasyMonkeyDevice from com.android.monkeyrunner.easy import By easyDevice = EasyMonkeyDevice(device) showDialogButton = By.id('id/show_dialog_button') if showDialogButton: easyDevice.touch(showDialogButton, MonkeyDevice.DOWN_AND_UP) ViewClient.sleep(3) editText = By.id('id/0x123456') print editText easyDevice.type(editText, 'Donald') ViewClient.sleep(3) ok = By.id('id/button1') if ok: # 09-08 20:16:41.119: D/MonkeyStub(1992): translateCommand: tap 348 268 easyDevice.touch(ok, MonkeyDevice.DOWN_AND_UP) hello = By.id('id/hello') if hello: if easyDevice.getText(hello) == "Hello Donald": print "OK" else: print "FAIL" else: print >> sys.stderr, "'hello' not found"
txtNumber = By.id('id/txtNumber') if not txtNumber: raise Exception("View id/txtNumber not found") # --------------------------------------------------------------------- # # --------------------------------------------------------------------- # # Run this for 100 random inputs for x in range(0, 100): # input is randomized, can be replaced with a command line array, etc. enter = int(random.randrange(-100,100)) MonkeyRunner.sleep(pause) # record the original number and current player old = int(easyDevice.getText(txtNumber)) player = easyDevice.getText(txtPlayer) # enter the input easyDevice.type(editGuess, str(enter)) MonkeyRunner.sleep(pause) print '%s: old = %s, enter = %s' % (player, old, enter) # simulate press on btnPost easyDevice.touch(btnPost, MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(pause) # fetch new values actual = int(easyDevice.getText(txtNumber))