自己的Python库

本文介绍了Android自动化测试中的关键操作,包括日志抓取、清除、截图保存、多线程使用以及unittest测试套件的实现。此外,还讲解了如何通过坐标定位点击UI、处理列表操作以及判断页面滑动状态。最后,提到了图片内容比较的方法。
摘要由CSDN通过智能技术生成

1.抓log

def logcat_start():
    current = os.getcwd()    //获得当前位置
    adblogcat_location = current         //定义当前位置
    adblogname = r"adb_log_" + time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) + ".txt"
 //  在Python的string前面加上‘r’, 是为了告诉编译器这个string是个raw string,不要转意backslash ‘’ 。 例如,\n 在raw string中,是两个字符,\和n, 而不会转意为换行符。由于正则表达式和 \ 会有冲突,因此,当一个字符串使用了正则表达式后,最好在前面加上’r’。          
 //“adb_log_"代表生成日志文件名字抬头,time.strftime代表生成日志时间”,“.txt"代表文件类型
   
    os.system("cd %s && adb logcat >>%s 2>&1" % (adblogcat_location, adblogname))//将日志文件保存到当前目录

2.清除log

def logcat_close():
    cmd = 'adb logcat -c'     //adb命令“清除日志”
    os.system(cmd)           //python调用上述cmd命令

3.截图连接设备并保存

def sceen():
   screenname = r"screen" + f"{变量值}" + time.strftime("Y-%m-%d-%H_%M_%S", time.localtime(time.time())) + ".png"//"scrren"为文件抬头名,time.strftime为截图时的时间,‘.png'为文件类型,f后跟的是可以变化的变量值,比如for循环的次数
   driver.get_screenshot_as_file(screenname)  //截图并保存时间
   备注:global x ,global后的代码都可调用x

4.多线程的使用-之后要加一个多线程详解

    def G6SATest04(self):
        global Test04time              #定义一个作用域为之后所有代码的变量
        for Test04time in range(3):
            threads = []
            threads.append(threading.Thread(target=logcat_start04))
            threads.append(threading.Thread(target=run04))
            print(threads)
            if __name__ == '__main__':
                for t in threads:
                    t.setDaemon(True)  # 加了这个是说明主线程执行完子线程也停止(无论是否执行完毕)
                    t.start()
                t.join()

5.unittest+suite实现方法可以按顺序执行

if __name__ == '__main__':
    suite = unittest.TestSuite()  # 创建测试套件
    suite.addTest(Testcase("G6SATest01"))
    suite.addTest(Testcase("G6SATest02"))
    suite.addTest(Testcase("G6SATest03"))  # 添加测试用例
    suite.addTest(Testcase("G6SATest04"))
    runner = unittest.TextTestRunner()
    runner.run(suite)

6.通过坐标定位点击UI

 x = 1026
 y = 290      //坐标点数值
 os.popen("adb shell input tap " + str(x) + " " + str(y))  #通过adb命令执行

7.删去1个&多个字符

string = '1,3,4,21,23,45'
list_string = list(string)#先获取字符串的序列
list_string.pop(0)#删去第0个字符
del list_string[0:12]#删去第0个到第12个字符

8.判断页面是否滑动到底部

#思想:获取滑动前的页面和滑动后的页面进行对比,如果两个页面完全相同,那么就是滑动到底部

before_swipe = driver.page_source#获取滑动前的页面
driver.swipe(x1,x2,y1,y2,duration)#滑动
after_swipe = driver.page_source#获取滑动后的页面
Swiped = True
if before_swipe == after_swipe:#判断条件
   Swiped = False #代表到底部了
else:
   Swiped = True
    

9.滑动,通过坐标

driver.swipe(start_x,start_y,end_x,end_y,duration)

10.定位一个元素&定位一组元素

s = driver.find_element_by_(link_text,id,)("xxxxx")#定位1个元素
s = driver.fing_element_by_xpath('//*[@text="xxxx"]')#通过path定位更加准确
s = driver.find_elements_by_class_name("xxxx")#定位一组元素,xxxx需要为这组元素共同拥有的属性

11.定位元素中的(“ x”)x为变量时

List = [1,2,3,4,5]
for i in range(0,len(List)):
    x = driver.find_element_by_xpath("//input[@text = '%s']" % List[i])

12.列表去重&按原来列表排序

List1 = list(set(List2)) #列表去重,List1是去重后的列表
List1.sort(key=device_filenameList2.index)#List1保持List2的排序顺序

13.判断某个元素是否存在于列表

x = '2'
List = ['1','2']
if x in List:#判断x是否存在于List
  print(x + "PASS")
else:
  print(x + "FAIL") 

14.向列表中添加元素

List.append(x)

15.比较两张图片内容是否相同

from PIL import Image
import math
import operator
from functools import reduce

def compare(pic1, pic2):
    '''
    :param pic1: 图片1路径
    :param pic2: 图片2路径
    :return: 返回对比的结果
    '''
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a, b: (a-b)**2, histogram1, histogram2)))/len(histogram1))
    print(differ)#differ为0.0时,两张图片完全相同
    return differ
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值