monkeyrunner脚本实践

本文介绍了一种使用MonkeyRunner进行自动化测试的方法,包括程序登录、新建患者信息及删除患者记录等功能的实现流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现程序公司程序的登录、新建患者、删除新建患者的功能
#  -*- coding:utf-8 -*-  
import sys
from com.android.monkeyrunner import MonkeyRunner as MR
from com.android.monkeyrunner import MonkeyDevice as MD
from com.android.monkeyrunner import MonkeyImage as MI
from com.android.monkeyrunner.easy import EasyMonkeyDevice
from com.android.monkeyrunner.easy import By
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
from com.android.hierarchyviewerlib.models import ViewNode, Window

############获取viewList中节点对象
def getChildView(parentId, *childSeq):
    childView="hierarchyViewer.findViewById('" + parentId +"')"
    for index in childSeq:
        childView += ('.children[' + str(index) + ']')
    print childView
    return eval(childView)
##########获取viewList中某个节点的中心点坐标
def getListViewPoint(parentId, *childSeq):
    list_node = getChildView(parentId, *childSeq)
    list_node_point = device.getHierarchyViewer().getAbsoluteCenterOfView(list_node)
    return list_node_point
###########获取viewList中某个节点相对屏幕的中心点坐标(绝对坐标)
def getListViewAbsolutePoint(list_node, contentWidth, contentHeight, taskbarHeight):
    screen_height = int(device.getProperty('display.height'))  # 得到屏幕Y轴高度
    screen_width = int(device.getProperty('display.width'))  # 得到屏幕X轴高度
    list_node.x = list_node.x + (screen_width - contentWidth) / 2
    list_node.y = list_node.y + (screen_height - contentHeight) / 2 + taskbarHeight / 2
    return list_node
############选择ListViwe中的一条记录
def getListViewItem(contentId, taskbarHeight,parentId, *childSeq):
    sl_depart_point = getListViewPoint(parentId, *childSeq)  # 获取科室列表第一个节点的相对中心坐标
    currentContent = hierarchyViewer.findViewById(contentId)  # 获取科室选择列表的content
    sl_depart_point = getListViewAbsolutePoint(sl_depart_point, currentContent.width, currentContent.height,
                                               taskbarHeight)  ##获取科室列表第一个节点的绝对中心坐标
    device.touch(sl_depart_point.x, sl_depart_point.y, MD.DOWN_AND_UP)
    return  None
###########获取控件的中心点对象
def getControlPoint(controlId):
    control =  hierarchyViewer.findViewById(controlId)
    control_point = device.getHierarchyViewer().getAbsoluteCenterOfView(control)
    return control_point
###########软件登录
def login():
    componentName = 'com.medex.Activity/.Login'
    device.startActivity(component=componentName)  # 启动应用程序
    MR.sleep(1.0)

    easyMonkey.touch(By.id("id/btn_department_more"), MD.DOWN_AND_UP)  # 点击科室右侧下拉框按钮
    MR.sleep(0.5)
    getListViewItem('id/content', taskbarHeight,  'id/select_dialog_listview', 0)  # 选择科室列表中的第一条记录
    MR.sleep(0.5)
    easyMonkey.touch(By.id("id/btn_account_more"), MD.DOWN_AND_UP)  # 点击用户名右侧的下拉框按钮
    MR.sleep(0.5)
    getListViewItem('id/content', taskbarHeight,  'id/select_dialog_listview', 0)  # 选择科室列表中的第一条记录
    MR.sleep(0.5)
    easyMonkey.touch(By.id("id/txtPwd"), MD.DOWN_AND_UP)  # 点击密码输入框
    device.press('KEYCODE_BACK', 'DOWN_AND_UP')
    device.type('507507')  # 密码输入框中填写'507507'
    btnLogin_point = getListViewPoint('id/login_btns', 0)  # 获取登录按钮的poinit
    device.touch(btnLogin_point.x, btnLogin_point.y, MD.DOWN_AND_UP)  # 点击登录按钮
    return None
###########新建患者
#参数说明:0:姓名 1:性别 2:年龄
def newPatient(*patientInfo):
    btnNewPatient_point = getControlPoint("id/newCase")  #获取新建患者按钮中心坐标对象
    device.touch(btnNewPatient_point.x, btnNewPatient_point.y, MD.DOWN_AND_UP)
    MR.sleep(1.0)
    currentContent = hierarchyViewer.findViewById("id/content") #获取新建患者界面content对象
    #输入姓名
    txtPatientName_point = getListViewPoint("id/grid_view_new", 1, 1) #获取新建患者界面姓名输入框的中心坐标对象
    txtPatientName_point = getListViewAbsolutePoint(txtPatientName_point, currentContent.width, currentContent.height, taskbarHeight)
    #由于获取到的id/content容器与实际根级容器宽度小40 高度小35
    device.touch(txtPatientName_point.x - 20, txtPatientName_point.y -18, MD.DOWN_AND_UP)
    MR.sleep(5)
    device.press('KEYCODE_BACK', 'DOWN_AND_UP')
    device.type(patientInfo[0])
    #选择性别
    if patientInfo[1] == "男":
        rbtnSex_point = getListViewPoint("id/rg_gender", 0) #获取性别男单选框相对坐标对象
    else:
        rbtnSex_point = getListViewPoint("id/rg_gender", 1) #获取性别女单选框相对坐标对象
    rbtnSex_point = getListViewAbsolutePoint(rbtnSex_point, currentContent.width, currentContent.height, taskbarHeight)
    device.touch(rbtnSex_point.x - 20, rbtnSex_point.y - 18, MD.DOWN_AND_UP)
    #输入年龄
    txtAge_point = getListViewPoint("id/grid_view_new", 5, 1) #获取年龄输入框相对坐标
    txtAge_point = getListViewAbsolutePoint(txtAge_point, currentContent.width, currentContent.height, taskbarHeight)
    device.touch(txtAge_point.x - 20, txtAge_point.y - 18, MD.DOWN_AND_UP)
    device.press('KEYCODE_BACK', 'DOWN_AND_UP')
    device.type(patientInfo[2])
    #选择年龄单位
    if patientInfo[3] == "岁":
        rbtnAgeUnit_point = getListViewPoint("id/rg_age", 0) #获取年龄单位岁单选框相对坐标对象
    elif patientInfo[3] == "月":
        rbtnAgeUnit_point = getListViewPoint("id/rg_age", 1) #获取年龄单位月单选框相对坐标对象
    else:
        rbtnAgeUnit_point = getListViewPoint("id/rg_age", 2) #获取年龄单位天单选框相对坐标对象
    rbtnAgeUnit_point = getListViewAbsolutePoint(rbtnAgeUnit_point, currentContent.width, currentContent.height, taskbarHeight)
    device.touch(rbtnAgeUnit_point.x - 20, rbtnAgeUnit_point.y - 18, MD.DOWN_AND_UP)
    #点击保存按钮
    btnSave_point = getControlPoint("id/title_btnRight")
    txtAge_point = getListViewAbsolutePoint(btnSave_point, currentContent.width, currentContent.height, taskbarHeight)
    device.touch(btnSave_point.x - 20, btnSave_point.y - 18, MD.DOWN_AND_UP)
    return None
###########删除病历列表第一条数据
def deletePatient():
    listItem1 = getListViewPoint("id/list", 0)
    device.drag((listItem1.x, listItem1.y), (listItem1.x - 600, listItem1.y), 1, 2) #从病历列表第一条的中心点左滑动600像素
    btnItemDelete = getListViewPoint("id/list", 0, 1, 2)
    device.touch(btnItemDelete.x, btnItemDelete.y, MD.DOWN_AND_UP)

    MR.sleep(0.5)
    btnYes = getControlPoint("id/button1")
    currentContent = hierarchyViewer.findViewById("id/content")  #获取删除病历提示窗口content对象
    btnYes = getListViewAbsolutePoint(btnYes, currentContent.width, currentContent.height, taskbarHeight)
    device.touch(btnYes.x, btnYes.y, MD.DOWN_AND_UP) #点击是按钮

device = MR.waitForConnection()
if not device:
    print >> sys.stderr, "connect device fail!"
    sys.exit(1)
easyMonkey = EasyMonkeyDevice(device)
if not easyMonkey:
    print >> sys.stderr, "connect easyMonkey device fail!"
    sys.exit(1)
hierarchyViewer = device.getHierarchyViewer()
print "device is connected"
screenContent = hierarchyViewer.findViewById('id/xy_search')  # 获得整个屏幕显示区域的节点信息
screenContent_loca = device.getHierarchyViewer().getAbsolutePositionOfView(screenContent)  # 获取整个屏幕显示区域的左上角坐标
taskbarHeight = screenContent_loca.y  # 获取任务栏的高度
print "takebar's heigt is: ", taskbarHeight
login()
print "Login completed"
MR.sleep(2)
#参数说明:0:姓名 1:性别(男、女) 2:年龄 3:年龄单位(岁、月、日)
listPatientInfo = ["test", "男", "11", "月"]
newPatient(*listPatientInfo)
print "NewPatient completed"
MR.sleep(1)
deletePatient()
print "delete first item in list completed"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值