class MonkeyWrapImpl(): def waitForConnection(self): self.device = MonkeyRunner.waitForConnection() self.initEasyDevice() def startActivity(self, runComponent): self.device.startActivity(component=runComponent) def initEasyDevice(self): self.easyDevice = EasyMonkeyDevice(self.device) def touchByViewId(self, viewId): self.easyDevice.touch(By.id(viewId), MonkeyDevice.DOWN_AND_UP) def visible(self, viewId): self.easyDevice.visible(By.id(viewId)) def touch_down(self, x, y): self.device.touch(x, y, MonkeyDevice.DOWN) def touch_up(self, x, y): self.device.touch(x, y, MonkeyDevice.UP) def touch_down_and_up(self, x, y): self.device.touch(x, y, MonkeyDevice.DOWN_AND_UP) def press_menu_down_and_up(self): self.device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP) def press_power_down_and_up(self): self.device.press('KEYCODE_POWER', MonkeyDevice.DOWN_AND_UP) def drag(self, sx, sy, ex, ey, duration, steps): self.device.drag((sx, sy), (ex, ey), duration, steps) def wakeup(self): self.device.wake() def sleep(self, sec): MonkeyRunner.sleep(sec) def takeAndSaveSnapshot(self, savaPath, type): img = self.device.takeSnapshot() img.writeToFile(savaPath, type) def screenWidth(self): # w = self.device.getProperty('width') # print('width=' + w) # return w; return 720 def screenHeight(self): # h = self.device.getProperty('height') # return h; return 1280 def __init__(self): '''
class MonkeyWrapImpl(): def waitForConnection(self): self.device = MonkeyRunner.waitForConnection() self.initEasyDevice() def startActivity(self,runComponent): self.device.startActivity(component=runComponent) def initEasyDevice(self): self.easyDevice = EasyMonkeyDevice(self.device); def touchByViewId(self,viewId): self.easyDevice.touch(By.id(viewId),MonkeyDevice.DOWN_AND_UP) def visible(self,viewId): self.easyDevice.visible(By.id(viewId)) def touch_down(self,x,y): self.device.touch(x,y,MonkeyDevice.DOWN) def touch_up(self,x,y): self.device.touch(x,y,MonkeyDevice.UP) def touch_down_and_up(self,x,y): self.device.touch(x,y,MonkeyDevice.DOWN_AND_UP) def press_menu_down_and_up(self): self.device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP) def press_power_down_and_up(self): self.device.press('KEYCODE_POWER', MonkeyDevice.DOWN_AND_UP) def drag(self,sx,sy,ex,ey,duration,steps): self.device.drag((sx,sy),(ex,ey),duration,steps); def wakeup(self): self.device.wake() def sleep(self,sec): MonkeyRunner.sleep(sec); def takeAndSaveSnapshot(self,savaPath,type): img = self.device.takeSnapshot() img.writeToFile(savaPath,type) def screenWidth(self): # w = self.device.getProperty('width') # print('width=' + w) # return w; return 720; def screenHeight(self): # h = self.device.getProperty('height') # return h; return 1280; def __init__(self): '''
def main(): apk = sys.argv[1] package = sys.argv[2] instructions_file = sys.argv[3] print(instructions_file) print('Checking adb devices') devices = get_devices() print('Devices found: '+str(devices)) if not len(devices): print('No devices found, setting up emulator') run_emulator() print('Emulator boot completed.. proceding..') device = MonkeyRunner.waitForConnection() easy_device = EasyMonkeyDevice(device) print('Connected\nInstalling package..') device.installPackage(apk) print('Installed!') print('Checking all activities..\nThis may take a while..') f = open(instructions_file, 'r') instructions = json.load(f) for activity in instructions: print(activity) runComponent = package + '/' + activity for button in instructions[activity]: device.startActivity(component=runComponent) time.sleep(1) if easy_device.visible(By.id('id/'+button)): easy_device.touch(By.id('id/'+button), MonkeyDevice.DOWN_AND_UP) time.sleep(1) else: device.press("KEYCODE_BACK", MonkeyDevice.DOWN_AND_UP) time.sleep(1) if easy_device.visible(By.id('id/'+button)): easy_device.touch(By.id('id/'+button), MonkeyDevice.DOWN_AND_UP) result = device.takeSnapshot() result_path = os.path.join(os.path.abspath('monkeyresult/'), package) if not os.path.exists(result_path): os.makedirs(result_path) result.writeToFile( os.path.join(result_path, activity+'.png'), 'png' ) f.close() print('Saved some snapshots to\n'+result_path)
#!/usr/bin/env python # -*- coding: utf-8 -*- # coding:utf-8 import sys from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage from com.android.monkeyrunner.easy import EasyMonkeyDevice from com.android.monkeyrunner.easy import By import subprocess device = MonkeyRunner.waitForConnection(10.0, "127.0.0.1:21503") easyDevice = EasyMonkeyDevice(device) if True == easyDevice.visible(By.id('id/button1')): print "find" easyDevice.touch(By.id('id/button2'), MonkeyDevice.DOWN_AND_UP) print easyDevice.locate(By.id('id/button2')) MonkeyRunner.sleep(2)
class MonkeyLib: def __init__(self, apk, package, mainActivity, runActivity, bundle): # Connects to the current device, returning a MonkeyDevice object self.device = MonkeyRunner.waitForConnection() self.easy_device = EasyMonkeyDevice(self.device) self.apk = apk self.package = package self.bundle = bundle self.main = self.component(package, mainActivity) self.run = self.component(package, runActivity) def component(self, package, activity): return package + '/' + activity # I N S T A L L / U N I N S T A L L def install(self): # Installs the Android package. Notice that this method returns a boolean, so you can test to see if the installation worked. print('INSTALL APP ' + self.apk) self.device.installPackage(self.apk) def remove(self): print('REMOVE APP ' + self.package) self.device.removePackage(self.package) # S Y N C def waitVisible(self, idstr): print('VISIBLE ' + idstr) v = By.id(idstr) while (not self.easy_device.visible(v)): print('wait ' + str(v)) #print('visible'); return v def sleep(self, s): MonkeyRunner.sleep(s) # A C T I V I T Y def mainActivity(self): print('MAIN') self.device.startActivity(component=self.main) self.sleep(3) def activity(self): print('ACTIVITY ' + str(self.run)) self.device.startActivity(component=self.run) self.sleep(3) def activity2(self, action, extras): print('ACTIVITY ' + str(self.run) + ' extras:' + str(extras)) extras.update(self.bundle) self.device.startActivity(component=self.run, action=action, extras=extras) self.sleep(3) def kill(self): print('KILL') self.device.shell('am force-stop ' + self.package) # S E A R C H def search(self, w): print('SEARCH ' + w) v = self.waitVisible('id/search') sv = By.id('id/search') self.easy_device.touch(sv, MonkeyDevice.DOWN_AND_UP) self.easy_device.type(sv, w) self.easy_device.press('KEYCODE_ENTER', MonkeyDevice.DOWN_AND_UP) self.sleep(3) # K E Y B O A R D E V E N T S def pressKey(self, k): print('PRESS ' + k) self.device.press(k, MonkeyDevice.DOWN_AND_UP) def back(self): print('BACK') pressKey('KEYCODE_BACK') # T O U C H E V E N T S def touch(self, id): print('TOUCH ' + id) bid = By.id(id) self.easy_device.touch(bid, MonkeyDevice.DOWN_AND_UP) def touchView(self, vn): print('TOUCH ' + str(vn)) p = self.device.getHierarchyViewer().getAbsoluteCenterOfView(vn) self.device.touch(p.x, p.y, MonkeyDevice.DOWN_AND_UP) def touchout(self): print('TOUCHOUT') self.device.touch(10, 100, MonkeyDevice.DOWN_AND_UP) def drag(self): print('DRAG') self.device.drag((600, 400), (300, 200), 2, 10) #self.sleep(3) # T R A V E R S A L def traverseFrom(self, start, process): process(start) if start.children != None: for node in start.children: self.traverseFrom(node, process) def rtraverseFrom(self, start, process): process(start) if start.children != None: for node in reversed(start.children): self.rtraverseFrom(node, process) def ytraverse(self, node): yield node if node.children != None: for child in node.children: for node2 in self.ytraverse(child): yield node2 def ytraverseFrom(self, start, process): for node in self.ytraverse(start): if process(node): break def yrtraverse(self, node): yield node if node.children != None: for child in reversed(node.children): for node2 in self.yrtraverse(child): yield node2 def yrtraverseFrom(self, start, process): for node in self.yrtraverse(start): if process(node): break # V I E W S def findViewById(self, id): return self.device.getHierarchyViewer().findViewById(id) #def findViewById2(self,id): # By.id(id).findView(self.device.getHierarchyViewer()) def getViewIds(self): return self.device.getViewIdList() # I M A G I N G def snapshot(self): # Takes a screenshot: a MonkeyImage return self.device.takeSnapshot() def toFile(self, shot, fname): # Writes the screenshot to a file shot.writeToFile('shot-' + fname + '.png', 'png')
string = "adb shell am broadcast -a ADB_INPUT_TEXT --es msg " + u"看起来很漂亮" + str( randint(0, 1000)) subprocess.check_call(string) MonkeyRunner.sleep(4) # 点击发布评论 easyDevice.touch(By.id('id/confirm'), MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(2) # 检查是否发布失败,失败就叉掉 if True == easyDevice.visible(By.id('id/close')): # 失败了 easyDevice.touch(By.id('id/close'), MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(4) easyDevice.touch(By.id('id/button1'), MonkeyDevice.DOWN_AND_UP) MonkeyRunner.sleep(4) # 点击返回 easyDevice.touch(By.id('id/top_left'), MonkeyDevice.DOWN_AND_UP)