Monkeyrunner contrast screen local image. Getsubimage ()
Monkeyrunner uses. Takesnapshot () When performing tests, by default intercepting the entire screen, including the system's status bar. The real phone status bar contains variables such as charge/semaphore/message prompt, which is prone to error when using. Sameas () to compare the entire screen. The use of. Getsubimage () to obtain a local image, and then contrast, reduces the probability of monkeyrunner execution result error.
Monkeyrunner uses getsubimage () to get a local image , as in the following example:
#第一个
Pic=device.takesnapshot ()
#获取指定区域的图像 (0,57,715,1123), note two brackets
Pic= Pic.getsubimage ((0,57,715,1123))
#第二并获取相同的局部图像
Newpic=device.takesnapshot ()
Newpic=newpic.getsubimage ((0,57,715,1123))
#使用. Sameas () compare two pictures and output the comparison result true or False
Print (Newpic.sameas (pic,1.0))
The specified range value in Newpic.getsubimage ((0,57,715,1123)) uses pixel perfect to get the coordinate point, or to get it locally, get the upper-left and lower-right coordinates first, and the top two values are left in the upper-left corner. The last two values are the coordinates of the lower-right corner minus the upper-left corner.
Complete Example: Open the specified app, two times and get an image of the specified area for comparison
From Com.android.monkeyrunner import Monkeyrunner,monkeydevice,monkeyimage
#连接android模拟器
device = monkeyrunner.waitforconnection (5, ' emulator-5554′)
#打开已安装的应用
Device.startactivity (component= ' Com.estrongs.android.pop/.view. Fileexploreractivity ')
#等待5s
Monkeyrunner.sleep (5)
#第一个
Pic=device.takesnapshot ()
#获取指定区域的图像 (0,57,715,1123), note two brackets
Pic= Pic.getsubimage ((0,57,715,1123))
#第二并获取相同的局部图像
Newpic=device.takesnapshot ()
Newpic=newpic.getsubimage ((0,57,715,1123))
#使用. Sameas () compare two pictures and output the comparison result true or False
Print (Newpic.sameas (pic,1.0))
#将指定区域的图像保存到本地文件夹
Pic.writetofile ('./shot.png ', ' png ')
Newpic.writetofile ('./shot1.png ', ' png ')
Monkeyrunner contrast screen local image. Getsubimage ()