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) )
7.删去1个&多个字符
string = '1,3,4,21,23,45'
list_string = list ( string)
list_string. pop( 0 )
del list_string[ 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" )
s = driver. fing_element_by_xpath( '//*[@text="xxxx"]' )
s = driver. find_elements_by_class_name( "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. sort( key= device_filenameList2. index)
13.判断某个元素是否存在于列表
x = '2'
List = [ '1' , '2' ]
if x in 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)
return differ