其他测试框架 monkeyrunner 脚本增强 1

bling8090 · 2019年04月17日 · 1093 次阅读

1、实现需求:打开"联系人",从既有列表中随机选取测试数据,新建联系人,批量执行 100 次并完成后截图保存到本地。
2、语言:Python
3、工具:pycharm

# -*- coding: UTF-8 -*-
#!usr/bin/python
#   adb shell dumpsys window w | findstr \/ | findstr name=
#   com.android.dialer/com.android.dialer.DialerAliasActivity

##  新建一个联系人,并保存,截图图片。
import time
import random
import os
import string

from com.android.monkeyrunner import MonkeyRunner,MonkeyImage,MonkeyDevice
device = MonkeyRunner.waitForConnection()
act = 'com.android.dialer/com.android.dialer.DialerAliasActivity'
second = 1
screenPath  = 'E:\\screenshot\\F5\\'
now = time.strftime("%Y-%m-%d-%H-%M-%S")
list_str = ['ab', 'cd', 'ef', 'gh', 'ij', 'kl', 'mn', 'op', 'qr', 'st',
            'uvw', 'xy', 'zz', 'qewe', '360', '&%¥','erer', 'fgsf', ]
list_tel = ['1234','5678','7890','2345','3456','4567','5678','6789',
            '7899','5656','6767','7878','9090','8989','123123',
            '343423','4545','45432','4321','5432','6543','7654','8765']
for x in range(1,101):
    if device:
        device.startActivity(act)
        MonkeyRunner.sleep(second)
    device.touch(651,103,'DOWN_AND_UP')
    MonkeyRunner.sleep(second)
    device.touch(580,180,'DOWN_AND_UP')
    MonkeyRunner.sleep(second)

    ###   从列表中选择几个拼接成联系人姓名,并输入
    #  这个方法:嵌套循环由于break了所以只能取出第一个元素
    # for x in list_str:
    #     for y in  list_str:
    #         select_elm = x+y
    #         device.type(select_elm)
    #         break
    #     break
    #  方法1:
    # select_str1 = random.choice(list_str)
    # select_str2 = random.choice(list_str)
    # join_str = select_str1 + select_str2
    # device.type(join_str)
    #  方法2:
    select_list_elm = random.sample(list_str,2)#  随即从列表中选出两个元素组成新的列表
    new_str = ''.join(select_list_elm)
    # new_str = string.join(select_list_elm).replace(' ',' ')
    device.type(new_str)


    device.touch(179,310,'DOWN_AND_UP')
    #  从列表中随机选3个元素组成号码,并输入
    select_tel = random.sample(list_tel,3)
    new_tel = ''.join(select_tel)
    device.type(new_tel)

    device.touch(651,97,'DOWN_AND_UP')
    MonkeyRunner.sleep(2)
    device.takeSnapshot().writeToFile(screenPath + now + '.jpg','jpg')
    device.press('KEYCODE_HOME','DOWN_AND_UP')
    device.press('KEYCODE_HOME','DOWN_AND_UP')
    MonkeyRunner.sleep(2)


    #  日志截取
    path_temp = os.path.split(os.path.realpath(__file__))[0]
    current_path = path_temp.replace("\\", "/")
    filename = current_path + "/testlog.log"
    f = open(filename, 'a')
    timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    print(timestamp)
    f.write(timestamp)
    f.close()

4、编写中注意的问题:
(1)中文需要 utf-8
(2)批量执行 100 次:for xinrange(1,101):
(3)随机:random
(4)由于未设置 sleep 的时间或者时间较短,导致截图的图片不一致问题

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册