Monkeyrunner contrast screen local image. Getsubimage ()

Source: Internet
Author: User

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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.