Android自动化测试之MonkeyRunner之MonkeyImage

A monkeyrunner class to hold an image of the device or emulator's screen. The image is copied from the screen buffer during a screenshot. This object's methods allow you to convert the image into various storage formats, write the image to a file, copy parts of the image, and compare this object to other MonkeyImage objects.

You do not need to create new instances of MonkeyImage. Instead, useMonkeyDevice.takeSnapshot() to create a new instance from a screenshot. For example, use:

newimage = MonkeyDevice.takeSnapshot()

Summary

Methods
string convertToBytes( string format)Converts the current image to a particular format and returns it as astring that you can then access as aniterable of binary bytes.
tuple getRawPixel( integer x, integer y)Returns the single pixel at the image location (x,y), as an atuple ofinteger, in the form (a,r,g,b).
integer getRawPixelInt( integer x, integer y)Returns the single pixel at the image location (x,y), as a 32-bitinteger.
MonkeyImage getSubImage( tuple rect)Creates a newMonkeyImage object from a rectangular selection of the current image.
boolean sameAs( MonkeyImage other, float percent)Compares thisMonkeyImage object to another and returns the result of the comparison. Thepercent argument specifies the percentage difference that is allowed for the two images to be "equal".
void writeToFile( string path, string format)Writes the current image to the file specified by filename, in the format specified byformat.

Public Methods

string convertToBytes(string format)

Converts the current image to a particular format and returns it as a string that you can then access as aniterable of binary bytes.

Arguments
formatThe desired output format. All of the common raster output formats are supported. The default value is "png" (Portable Network Graphics).

tuple getRawPixel(integer x,integer y)

Returns the single pixel at the image location (x,y), as an a tuple ofinteger, in the form (a,r,g,b).

Arguments
xThe horizontal position of the pixel, starting with 0 at the left of the screen in the orientation it had when the screenshot was taken.
yThe vertical position of the pixel, starting with 0 at the top of the screen in the orientation it had when the screenshot was taken.
Returns
  • A tuple of integers representing the pixel, in the form (a,r,g,b) where a is the alpha channel value, and r, g, and b are the red, green, and blue values, respectively.

tuple getRawPixelInt(integer x,integer y)

Returns the single pixel at the image location (x,y), as an an integer. Use this method to economize on memory.

Arguments
xThe horizontal position of the pixel, starting with 0 at the left of the screen in the orientation it had when the screenshot was taken.
yThe vertical position of the pixel, starting with 0 at the top of the screen in the orientation it had when the screenshot was taken.
Returns
  • The a,r,g, and b values of the pixel as 8-bit values combined into a 32-bit integer, with a as the leftmost 8 bits, r the next rightmost, and so forth.

MonkeyImagegetSubImage(tuple rect)

Creates a new MonkeyImage object from a rectangular selection of the current image.

Arguments
rectA tuple (x, y, w, h) specifying the selection. x and y specify the 0-based pixel position of the upper left-hand corner of the selection. w specifies the width of the region, and h specifies its height, both in units of pixels.

The image's orientation is the same as the screen orientation at the time the screenshot was made.

Returns
  • A new MonkeyImage object containing the selection.

boolean sameAs(MonkeyImageotherImage,float percent )

Compares this MonkeyImage object to another and returns the result of the comparison. Thepercent argument specifies the percentage difference that is allowed for the two images to be "equal".

Arguments
otherAnother MonkeyImage object to compare to this one.
percentA float in the range 0.0 to 1.0, inclusive, indicating the percentage of pixels that need to be the same for the method to returntrue. The default is 1.0, indicating that all the pixels must match.
Returns
  • Boolean true if the images match, or boolean false otherwise.

void writeToFile(string filename,string format)

Writes the current image to the file specified by filename, in the format specified byformat.

Arguments
pathThe fully-qualified filename and extension of the output file.
formatThe output format to use for the file. If no format is provided, then the method tries to guess the format from the filename's extension. If no extension is provided and no format is specified, then the default format of "png" (Portable Network Graphics) is used.

参考:http://developer.android.com/tools/help/MonkeyImage.html

更多相关文章

  1. Android单元测试和日志输出
  2. google Android 认证测试
  3. android 7.0 系统关闭彩信过CTA测试的方法
  4. android单元测试 配置注意
  5. Android 4.0.3 CTS 测试
  6. android之单元测试――上
  7. Android下的单元测试
  8. android CTS测试
  9. Android 开发笔记 4:用模拟器测试Android 镜像

随机推荐

  1. android 画线
  2. amlogic 编译 android
  3. Android(安卓)Overview
  4. android颜色管理
  5. android常用颜色
  6. Android的Activity启动流程分析
  7. android 的button
  8. TextView跑马灯效果
  9. android Activity
  10. Android