jiguanghover 发表于 2013-5-14 08:54:17

使用python 的subprocess调用monkey runner 脚本

本帖最后由 jiguanghover 于 2013-5-14 08:56 编辑

1. 我使用python的subprocess直接执行monkey runner脚本,可以实现图片的比对,代码如下:
import subprocess
subprocess.Popen("xxx/android-sdk-linux_x86/tools/monkeyrunnerAutoTesting/aaa/startApp.py", shell=True)

2. 以下是startApp.py的脚本:
from com.android.monkeyrunner import MonkeyRunner as mr,MonkeyDevice as md
import os
import sys

cate_name=["全部","居家","配饰"]
cate_x=
cate_y=


def startApp(d):
    my_pack="com.meishi.test"
    my_act=".SplashActivity"
    my_compon=my_pack+'/'+my_act
    d.startActivity(component=my_compon)
    mr.sleep(10)

def screenShot(d):
    result1=d.takeSnapshot()
    result1.writeToFile("meishi_image/st1.png","png")
   
def changeCategory(d):
    ''' Open category selection list '''
    #d.touch(160,50,md.DOWN_AND_UP)
    k=1
    rect=(80,30,150,30)
    mr.sleep(3)
    for i in xrange(3):
      for j in xrange(4):
            if i*j==6:
                print i,j
                break
            d.touch(160,50,md.DOWN_AND_UP)
            mr.sleep(2)
            d.touch(cate_x,cate_y,md.DOWN_AND_UP)
            mr.sleep(3)
            img=d.takeSnapshot().getSubImage(rect)
            img.writeToFile("meishi_image/shot%d.png"%k,"png")
            k+=1
            print "K is "+str(k)
         
                           
def execCommand(d):
    print "Start execute command"
    command="python compare_image.py"
    #command="ls -l"
    os.system(command)

def main():
    device=mr.waitForConnection()
    if not device:
      print "Device is not found!"
      sys.exit()
    else:
      print "Device is connected!"
      
    startApp(device)
    screenShot(device)
    changeCategory(device)
    execCommand(device)
   

if __name__=="__main__":
    main()

3. python有专门的图片比对代码,我就直接拿过来用了,以下是执行图片比对的脚本,compareImage.py:
import Image

def make_regalur_image(img, size = (256, 256)):
    return img.resize(size).convert('RGB')

def split_image(img, part_size = (64, 64)):
    w, h = img.size
    pw, ph = part_size
   
    assert w % pw == h % ph == 0
   
    return [img.crop((i, j, i+pw, j+ph)).copy() \
                for i in xrange(0, w, pw) \
                for j in xrange(0, h, ph)]

def hist_similar(lh, rh):
    assert len(lh) == len(rh)
    return sum(1 - (0 if l == r else float(abs(l - r))/max(l, r)) for l, r in zip(lh, rh))/len(lh)

def calc_similar(li, ri):
#    return hist_similar(li.histogram(), ri.histogram())
    return sum(hist_similar(l.histogram(), r.histogram()) for l, r in zip(split_image(li), split_image(ri))) / 16.0
            

def calc_similar_by_path(lf, rf):
    li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf))
    return calc_similar(li, ri)

def make_doc_data(lf, rf):
    li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf))
    li.save(lf + '_regalur.png')
    ri.save(rf + '_regalur.png')
    fd = open('stat.csv', 'w')
    fd.write('\n'.join(l + ',' + r for l, r in zip(map(str, li.histogram()), map(str, ri.histogram()))))
#    print >>fd, '\n'
#    fd.write(','.join(map(str, ri.histogram())))
    fd.close()
    import ImageDraw
    li = li.convert('RGB')
    draw = ImageDraw.Draw(li)
    for i in xrange(0, 256, 64):
      draw.line((0, i, 256, i), fill = '#ff0000')
      draw.line((i, 0, i, 256), fill = '#ff0000')
    li.save(lf + '_lines.png')
   

if __name__ == '__main__':
    path = r'/AutoTesting/aaa/meishi_image/'
    for i in xrange(1,12):
      print calc_similar_by_path('./meishi_image/shot%d.png'%i,'./meishi_image/cate%d.png'%i)

audio0325 发表于 2013-8-19 23:25:32

图片对比,直接用monkeyrunner自带函数sameAS就可以了吧?还可以对指定的区域内进行截图对比

张亚洲 发表于 2013-12-11 09:15:32

你这图片对比代码太多,写个脚本还不上千行。
页: [1]
查看完整版本: 使用python 的subprocess调用monkey runner 脚本