def testlogin():  
    import codecs  
    codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)  
  
    device = MonkeyRunner.waitForConnection()  
    easyMonkey = EasyMonkeyDevice(device)  
  
    print ("Test start")  
    # 启动应用Activity
    device.shell('am start dong.sqlite.order/dong.sqlite.order.MainActivity')  
  
    MonkeyRunner.sleep(3)  
  
    # 输入账号
    print ("Start typing user name and password")
    by_name = By.id("id/username")
    easyMonkey.type(by_name,'zdd')  
    MonkeyRunner.sleep(1)  
    # 输入密码
    by_psd = By.id('id/userpwd')
    easyMonkey.type(by_psd,"my_psd")  
    MonkeyRunner.sleep(1)  
    device.press('KEYCODE_BACK')  
    MonkeyRunner.sleep(1)  
    # 点击登录
    by_save = By.id('id/btn_save')  
    easyMonkey.touch(by_save,MonkeyDevice.DOWN_AND_UP)  
    MonkeyRunner.sleep(2)

    print ("Click the read button")
    by_read = By.id('id/btn_read') 
    easyMonkey.touch(by_read,MonkeyDevice.DOWN_AND_UP)  
    MonkeyRunner.sleep(1) 
  
    device.press('KEYCODE_BACK')
    print ("Test end")
        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" 
            
#! /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'
Exemple #4
0
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')
Exemple #5
0
class fineMonkeyRunner:
    '''
    #############################
    finemonkeyrunner class
    #############################
    '''
    def __init__(self, emulatorname, switch=PHONE_EMULATOR_SWITCH):
        device = self.waitforconnection(waitForConnectionTime, emulatorname)
        self.debug(
            "__int__: creating the finemonkeyrunner object with emulatorname %s"
            % emulatorname)
        # use EasyMonkeyDevice and MonkeyDevice
        self.easydevice = EasyMonkeyDevice(device)
        self.device = device
        # presskey type
        self.DOWN = self.device.DOWN
        self.UP = self.device.UP
        self.DOWN_AND_UP = self.device.DOWN_AND_UP
        self.switch = switch
        self.debug('created the fineEasyDevice')
        self.viewlist = []

    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 sleep(self, seconds):
        self.debug('sleeping %f seconds' % seconds)
        mr.sleep(seconds)

    def installapp(self, apppath):
        try:
            self.device.installPackage(apppath)
        except:
            self.error("Install app Fail ")

    def remove(self, packagename):
        try:
            self.device.removePackage(packagename)
        except:
            self.error("Uninstall app Fail ")

    '''建立device连接'''

    def waitforconnection(self, waitForConnectionTime, emulatorname):
        try:
            self.debug("waitforconnectiontime:Success")
            return mr.waitForConnection(waitForConnectionTime, emulatorname)
        except:
            self.error("waitForConnection error")
            sys.exc_info()
            traceback.print_exc()
            return None

    ''' 启动应用的activity'''

    def startactivity(self, activity):
        try:
            self.debug("starting the activity... %s" % activity)
            self.easydevice.startActivity(component=activity)
        except:
            self.error("starting the activity %s error" % activity)
            sys.exc_info()
            traceback.print_exc()
            return False

    '''指定的id元素是否存在'''

    def isexist(self, id):
        self.debug('check the id is exist or not')
        try:
            if (self.easydevice.exists(self.getviewbyid(id))):
                return True
            else:
                self.debug(
                    'isexist: %s this id does not exists,will try check again'
                    % id)
                self.sleep(1)
        except:
            # self.debug('isexist: the %dst time check id (%s) existing error ,  , will retry ' % (tmp, id))
            self.sleep(1)

        return False

    '''对某一个键按多次'''

    def presskey(self, key, times, type):
        # self.device.press('KEYCODE_ENTER', MonkeyDevice.DOWN_AND_UP)
        for i in range(times):
            self.device.press(key, type)
        self.sleep(1)

    '''根据ID长按'''

    def longpressbyid(self, id, x=0, y=0):
        self.debug('calling longpressbyid function')

        hierarchyviewer = self.device.getHierarchyViewer()
        view = hierarchyviewer.findViewById(id)
        point = hierarchyviewer.getAbsoluteCenterOfView(view)
        item_btn_x = point.x + x
        item_btn_y = point.y + y
        self.device.touch(item_btn_x, item_btn_y, self.DOWN)
        self.sleep(1)
        self.device.touch(item_btn_x, item_btn_y, self.UP)
        return True

    '''根据view长按'''

    def longpressbyview(self, view, x=0, y=0):
        self.debug('calling longpressbyview function')
        hierarchyviewer = self.device.getHierarchyViewer()

        point = hierarchyviewer.getAbsoluteCenterOfView(view)
        item_btn_x = point.x + x
        item_btn_y = point.y + y
        self.device.touch(item_btn_x, item_btn_y, self.DOWN)
        self.sleep(1)
        self.device.touch(item_btn_x, item_btn_y, self.UP)
        return True

    '''获view的信息--p'''

    def getviewinfo_text(self, view):
        # text = view.namedProperties.get('text:mText').value.encode('utf8')
        try:
            textproperty = view.namedProperties.get("text:mText").value.encode(
                'utf8')
        except:
            self.debug('%s has not text:mText Property!' % str(view))
        else:
            #self.debug('The text is %s ' % textproperty)
            return textproperty

    '''--p'''

    def getviewinfo_height(self, view):
        height = view.namedProperties.get('layout:getHeight()').value.encode(
            'utf8')
        return height

    '''--p'''

    def getviewinfo_width(self, view):
        width = view.namedProperties.get('layout:getWidth()').value.encode(
            'utf8')
        return width

    '''--p'''

    def getviewinfo_mleft(self, view):
        mleft = view.namedProperties.get('layout:mLeft').value.encode('utf8')
        return mleft

    '''-p'''

    def getviewinfo_mtop(self, view):
        mtop = view.namedProperties.get('layout:mTop').value.encode('utf8')

    '''--p'''

    def getviewinfo_mid(self, view):
        mid = view.namedProperties.get('mID').value  #.encode('utf8')
        return mid

    '''--p'''

    def getviewinfo_visible(self, view):
        visible = view.namedProperties.get('getVisibility()').value.encode(
            'utf8')
        if visible == 'VISIBLE':
            self.debug('visible')
            return True
        else:
            return False

    '''--p'''

    def getviewinfo_xy(self, view):
        hierarchyviewer = self.device.getHierarchyViewer()
        point = hierarchyviewer.getAbsoluteCenterOfView(view)
        return point.x, point.y

    '''获取文本根据id'''

    def gettextbyid(self, id):
        if self.isexist(id):
            view = self.getviewbyid(id)
            viewtext = self.getviewinfo_text(view)
            return viewtext
        else:
            self.error('Not look for the element or it has a error!')
            return False

    '''获取view根据id --p'''

    def getviewbyID(self, id):
        starttime = time.time()
        print starttime
        hierarchyviewer = self.device.getHierarchyViewer()
        view = hierarchyviewer.findViewById(id)
        endtime = time.time()
        self.debug('Elapsed Iime:%f' % (endtime - starttime))
        return view

    '''获取view根据id --p'''

    def getviewbyid(self, id):
        self.debug('calling getviewbyid function by the id (%s)' % id)
        for tmp in range(repeatTimesOnError):
            try:
                return By.id(id)
            except:
                self.debug(
                    'getviewbyid: the %dst time error by id (%s) ,  will retry '
                    % (tmp, id))
                mr.sleep(1)
                continue
        self.error(
            'getviewbyid: sorry , still can\'t get the view by this id (%s). please check the view '
            % id)
        sys.exc_info()
        traceback.print_exc()
        return None

    '''根据层级结构来获取view,childseq表示层级的元组(1,1,0,0,2)---p'''

    def getchildviewbylayer(self, parentid, childseq):
        self.debug('calling getchildviewbylayer function')
        hierarchyviewer = self.device.getHierarchyViewer()
        childview = "hierarchyviewer.findViewById('" + parentid + "')"
        for index in childseq:
            childview += ('.children[' + str(index) + ']')
        #print childview
        try:
            eval(childview)
        except:
            self.debug('No find the view and please check childseq')
        else:
            return eval(childview)

    '''--p'''
    '''根据id返回该元素的一个x,y,h,w的元组'''

    def getelementinfo_locate(self, id):
        xyhw = self.easydevice.locate(By.id(id))
        return xyhw

    '''根据view获取类名---p'''

    def getviewinfo_classname(self, view):  #,view
        self.debug('calling traversalViewnode:%s ' % str(view))
        tmp = str(view).split('.')
        classname = tmp[-1].split('@')[0]
        return classname

    '''type:1是代表classname,type:2代表是id type:3代表是text ---p'''

    def traversalviewsametype(self, viewnode, name, type):
        # self.debug('calling traversalViewnode:%s '%str(viewnode))
        #tmplist = []
        for eachitem in range(len(viewnode.children)):
            tmpclassname = viewnode.children[eachitem]
            controls = 'TextView' in str(tmpclassname) or 'EditText' in str(
                tmpclassname) or 'tButton' in str(tmpclassname)
            if len(viewnode.children[eachitem].children) != 0:

                if type == 1 and (name in str(tmpclassname)):
                    self.viewlist.append(viewnode.children[eachitem])
                if type == 2 and self.getviewinfo_mid(
                        viewnode.children[eachitem]) == name:
                    self.viewlist.append(viewnode.children[eachitem])

                if type == 3 and controls and self.getviewinfo_text(
                        viewnode.children[eachitem]) == name:
                    self.viewlist.append(viewnode.children[eachitem])
                self.traversalviewsametype(viewnode.children[eachitem], name,
                                           type)
                # self.debug(result)
            else:
                if type == 1 and (name in str(tmpclassname)):
                    self.viewlist.append(viewnode.children[eachitem])
                if type == 2 and self.getviewinfo_mid(
                        viewnode.children[eachitem]) == name:
                    self.viewlist.append(viewnode.children[eachitem])
                if type == 3 and controls and self.getviewinfo_text(
                        viewnode.children[eachitem]) == name:
                    self.viewlist.append(viewnode.children[eachitem])

    '''获取指定id元素下的相同id的view对象列表 --p'''

    def getviewssameid(self, parentid, id):
        self.debug('calling getviewssameid function...')
        self.viewlist = []
        starttime = time.time()
        hierarchyviewer = self.device.getHierarchyViewer()
        viewnode = hierarchyviewer.findViewById(parentid)
        self.traversalviewsametype(viewnode, id, 2)
        endtime = time.time()
        self.debug('Elapsed Iime:%f' % (endtime - starttime))
        return self.viewlist
        #pass

    '''获取指定id元素下的相同classname的view对象列表 --p'''

    def getviewssameclassname(self, parentid, classname):
        self.debug('calling getviewssameclassname function...')
        self.viewlist = []
        starttime = time.time()
        hierarchyviewer = self.device.getHierarchyViewer()
        viewnode = hierarchyviewer.findViewById(parentid)
        self.traversalviewsametype(viewnode, classname, 1)
        endtime = time.time()
        self.debug('Elapsed Iime:%f' % (endtime - starttime))
        return self.viewlist

    '''获取子views根据父view ---p'''

    def getchildviewsbyparentview(self, parentview):
        self.debug('calling getchildviewsbyparentview function...')
        viewlists = []
        if len(parentview.children) != 0:
            for i in range(len(parentview.children)):
                viewlists.append(parentview.children[i])
            return viewlists
        else:
            self.debug('%s has not children view' % str(parentview))

    '''获取子view的数量 ---p'''

    def getchilrenlength(self, parentview):
        self.debug('calling getchilrenlength function...')
        return len(parentview.children)

    '''获取指定id元素下的相同文本的view对象列表 ---p'''

    def getviewssametext(self, parentid, text):
        self.debug('calling getviewssametext function...')
        self.viewlist = []
        starttime = time.time()
        hierarchyviewer = self.device.getHierarchyViewer()
        viewnode = hierarchyviewer.findViewById(parentid)
        self.traversalviewsametype(viewnode, text, 3)
        endtime = time.time()
        self.debug('Elapsed Iime:%f' % (endtime - starttime))
        return self.viewlist

    '''拖拽屏幕 ---p'''

    def dragscreen(self, start, end, duration, steps):
        self.debug('calling dragscreen function...')
        md.drag(start, end, duration, steps)

    '''在指定的id上输入文字--p'''

    def typebyid(self, id, content):
        self.debug('device input the %s by id' % content)
        self.easydevice.type(By.id(id), content)

    '''在当前焦点输入文字--p'''

    def type(self, content):
        self.debug('device input the %s' % content)
        self.device.type(content)

    '''点击元素根据view 对话框除外 ---p'''

    def clickelementbyview(self, view, x=0, y=0):
        self.debug('calling clickelementbyview function')
        for tmp in range(repeatTimesOnError):
            try:
                hierarchyviewer = self.device.getHierarchyViewer()
                point = hierarchyviewer.getAbsoluteCenterOfView(view)
                item_btn_x = point.x + x
                item_btn_y = point.y + y
                # print item_btn_x, item_btn_y
                self.debug('Click Position:x = %d ,y = %d' %
                           (item_btn_x, item_btn_y))
                self.device.touch(item_btn_x, item_btn_y, self.DOWN_AND_UP)
                return True
            except:
                self.debug(
                    'clickelementbyview: the %dst time click error , not found the view , will retry '
                    % tmp)
                if (tmp > 1 & DEBUG):
                    self.debug('Please wait to click the view')
                mr.sleep(1)
                continue
        self.error(
            'clickelementbyview: sorry , still can\'t click view. please check the view is exist or not , or increase the repeat times variable?'
        )
        sys.exc_info()
        traceback.print_exc()
        return False

    '''点击对话框中的view'''

    def clickdialogelementbyview(self, view):
        self.debug('calling clickdialogelementbyview function')
        hierarchyviewer = self.device.getHierarchyViewer()
        width = self.device.getProperty("display.width")
        height = self.device.getProperty("display.height")
        point = hierarchyviewer.getAbsoluteCenterOfView(view)
        p = view
        rootview = view
        while p != None:
            rootview = p
            p = p.parent
        x = (int(width) - int(rootview.width)) / 2 + point.x
        y = (int(height) - int(rootview.height)) / 2 + point.y
        self.debug('Position:x= %d ; y=%d' % (x, y))
        self.device.touch(x, y, self.DOWN_AND_UP)

        #print "clicked view"

    def clickbyviewondialog(self):
        pass

    '''根据文本点击元素,由于根据文本获取的是相同文本的view列表,默认使用第一个view,
    可以根据实际情况再调整,num代表view列表中的序号,默认0   非对话框 ---P'''

    def clickbytext(self, parentid, text, num=0):
        self.debug('calling clickbytext function')
        viewlist = self.getviewssametext(parentid, text)
        if viewlist != []:
            self.clickelementbyview(viewlist[num])
            return True
        else:
            self.debug('No find the view, please check')
            return None

    '''根据id点击元素 ---p'''

    def clickbyid(self, id, type):
        self.debug('calling clickbyid function')
        for tmp in range(repeatTimesOnError):
            try:
                self.easydevice.touch(By.id(id), type)
                return True
            except:
                self.debug(
                    'clickbyid: the %dst time click error by this id (%s) , not found the view , will retry '
                    % (tmp, id))
                if (tmp > 1 & DEBUG):
                    self.debug('Please wait to click the view')
                mr.sleep(1)
                continue
        self.error(
            'clickbyid: sorry , still can\'t click view. please check the view is exist or not , or increase the repeat times variable?'
        )
        sys.exc_info()
        traceback.print_exc()
        return False

    '''根据point点击元素 ---p'''

    def clickpoint(self, x, y, type):
        self.debug('calling click the point ')
        for tmp in range(repeatTimesOnError):
            try:
                self.device.touch(x, y, type)
                return True
            except:
                self.debug(
                    'clickpoint: %d time click point error , will retry ' %
                    tmp)
                mr.sleep(1)
                continue
        self.error(
            'clickpoint: sorry , still can\'t click point. please check the view is exist or not , or increase the repeat times variable?'
        )
        sys.exc_info()
        traceback.print_exc()
        return False

    '''判断给定的id元素是否是选中状态---p'''

    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)
                mr.sleep(1)
                continue
        self.error('isfocused: error occured')
        sys.exc_info()
        traceback.print_exc()
        return False

    '''获取指定的view文本 getviewinfo_text已经存在
    def gettextbyview(self, view):
        # if view != None:
        #  return (view.namedProperties.get('text:mText').value.encode('utf8'))
        textProperty = view.namedProperties.get("text:mText")
        if textProperty==None:
            self.debug('No text property on node')
            textProperty = view.namedProperties.get("mText")
            if textProperty == None:
                self.debug('No text property on node')
        return textProperty.value.encode('utf8')
    '''
    '''清除编辑框的文本 ---P'''

    def cleartextbyid(self, id):
        self.debug('calling cleartextbyid function by the id (%s)' % id)
        if self.isexist(id):
            if not self.isfocused(id):
                self.clickbyid(id, self.DOWN_AND_UP)
            textview = self.getviewbyID(id)
            rangenumber = len(self.getviewinfo_text(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

    '''根据id输入文本框内容 ---p'''

    def inputtextbyid(self, id, text):
        self.debug('calling inputtextbyid function by the id (%s)' % id)
        self.clickbyid(id, self.DOWN_AND_UP)
        if self.isfocused(id):
            self.type(text)
        else:
            self.debug('No Selected the element by id,Please check')

        if self.switch == 'E':
            self.debug('This is an emulator')
        else:
            self.presskey('KEYCODE_BACK', self.DOWN_AND_UP, 1)
        # 如果是真机需要打开语句,模拟器需关闭
        # self.pressKey('KEYCODE_BACK', self.DOWN_AND_UP, 1)
        # mr.sleep(1)
        return True

    '''根据当前界面上的任意id获取rootview ---p'''

    def getcurrentrootview(self, id):
        self.debug('calling getcurrentrootview function by the id (%s)' % id)
        hierarchyviewer = self.device.getHierarchyViewer()
        currentview = hierarchyviewer.findViewById(id)
        p = currentview
        rootview = currentview
        while p != None:
            rootview = p
            p = p.parent
        return rootview

    '''遍历viewnode节点,type=1是完全匹配type=0是包含关系'''

    def traversalviewnode(self, viewnode, text, type):
        # self.debug('calling traversalViewnode:%s '%str(viewnode))
        tmplist = []
        for eachitem in range(len(viewnode.children)):
            viewnodestr = str(viewnode.children[eachitem])
            if len(viewnode.children[eachitem].children) != 0:
                result = self.traversalviewnode(viewnode.children[eachitem],
                                                text, type)
                if result:
                    return self.traversalviewnode(viewnode.children[eachitem],
                                                  text, type)
            else:
                if 'TextView' in viewnodestr or 'EditText' in viewnodestr or 'tButton' in viewnodestr:
                    viewnodetext = self.getviewinfo_text(
                        viewnode.children[eachitem]
                    )  #viewnode.children[eachitem].namedProperties.get('text:mText').value.encode('utf8')
                    if type == 1 and viewnodetext == text:
                        # self.debug('1 Find the text:%s ' % viewnodetext)
                        tmplist.append('P')
                        return True

                    elif type == 0 and (text in viewnodetext):
                        # self.debug('0 Find the text: %s ' % viewnodetext)
                        tmplist.append('P')
                        return True
                    else:
                        tmplist.append('F')

    '''该元素内的文本是否包含指定的文本 ---p'''

    def elementshouldcontaintext(self, id, text, type):
        self.debug('calling elementshouldcontaintext function by the id (%s)' %
                   id)
        # 获取指定元id元素的viewnode
        starttime = time.time()
        hierarchyviewer = self.device.getHierarchyViewer()
        viewnode = hierarchyviewer.findViewById(id)
        result = self.traversalviewnode(viewnode, text, type)
        endtime = time.time()
        #print result
        if result:
            self.debug('Elapsed Iime:%f' % (endtime - starttime))
            return True
        else:
            self.debug('Elapsed Iime:%f' % (endtime - starttime))
            return False

    '''当前界面的文本是否包含指定的文本 ---p'''

    def pageshouldcontaintext(self, id, text, type):
        # 获取指定元id元素的rootview
        self.debug('calling pageshouldcontaintext function by the id (%s)' %
                   id)
        starttime = time.time()
        viewnode = self.getcurrentrootview(id)
        result = self.traversalviewnode(viewnode, text, type)
        endtime = time.time()
        #print result
        if result:
            self.debug('Elapsed Iime:%f' % (endtime - starttime))
            return True
        else:
            self.debug('Elapsed Iime:%f' % (endtime - starttime))
            return False

    '''判断id的元素文本是否相等---p'''

    def equaltextbyid(self, id, text):
        self.debug('calling equaltextbyid function by the id (%s)' % id)
        if self.isexist(id):
            view = self.getviewbyID(id)
            viewtext = self.getviewinfo_text(view)
            if text == viewtext:
                #self.debug('----Pass----')
                return True
            else:
                #self.debug('----Fail----')
                return False

    '''判断view的元素文本是否相等---p'''

    def equaltextbyview(self, view, text):
        self.debug('calling equaltextbyview function by the id (%s)' %
                   str(view))
        viewtext = self.getviewinfo_text(view)
        if text == viewtext:
            return True
        else:
            return False

    '''判断activity是否与预期的相同
    def assertcurrentactivity(self,currentactivity):
        starttime = time.time()
        self.debug('calling assertcurrentactivity function ')
        try:
            activity = self.device.shell('dumpsys activity | findstr "mFocusedActivity"') #adb shell
        except IOError:
            # print '获取当前activity失败'
            self.debug('获取当前activity失败')
        else:
            print activity
            tmp = activity.split(' ')
            activity = tmp[3]
            self.debug('activity : %s' % activity)
            if activity == currentactivity:
                endtime = time.time()
                times = endtime - starttime
                self.debug('Execute time is %d' % times)
                return True
            else:
                return False
    '''
    '''等待期望的activity出现,默认时间20秒 ---p'''

    def waitforactivity(self, waitactivity, repeatTimesOnError=20):
        for tmp in range(repeatTimesOnError):
            if self.assertfocusedwindowmame(waitactivity):
                return True
            else:
                self.debug(
                    'waitforactivity: %s this waitactivity does not exists,will try check again'
                    % waitactivity)
                mr.sleep(1)
                continue

    '''判断activity是否相等 --p'''

    def assertfocusedwindowmame(self, expectactivity):
        starttime = time.time()
        #hierarchyviewer = self.device.getHierarchyViewer()
        winId = self.easydevice.getFocusedWindowId()
        if winId == expectactivity:
            self.debug('%s is correct' % winId)  #.encode('utf-8')
            endtime = time.time()
            times = endtime - starttime
            self.debug('Execute time is %d' % times)
            return True
            #print winId.encode('utf-8')
        else:
            self.debug('%s is wrong' % winId)
            return False

    '''--p'''
    '''Force to stop the App'''

    def forcestopapp(self, packagename):

        self.debug('calling forcestopapp function ')
        try:
            activity = self.device.shell('am force-stop %s' % packagename)
            return True
        except IOError:
            self.debug('force stop the app')
                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"
Exemple #7
0
# --------------------------------------------------------------------- #
# --------------------------------------------------------------------- #
# 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))
    expected = old + enter

    # check against expected
    if actual == expected: