在实际测试中有时候会遇到一些开关按钮,但是这些按钮的checkable属性为false,很难从控件属性上判定开关状态的时候,我们可以根据开关的当前颜色来判定。
比如:
所以,这就需要获取控件的坐标的颜色值,随后通过颜色值来判定开关状态。
/**
* 获取给定图片的指定坐标点的RGB值,结果以16进制输出(例:ffffff)
*
* @param picPath 需要获取像素点的图片地址
* @param x 像素点的横坐标值
* @param y 像素点的纵坐标值
* @return 16进制的RGB值,以String类型返回。例:ffffff
* @throws AutoException
* @author ZeKyll
*/
public String getRGB(String picPath, int x, int y) throws AutoException {
LogTimeGetter.updateTimeString();
BitmapFactory.Options op = new BitmapFactory.Options();
op.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap targetBitmap = BitmapFactory.decodeFile(picPath, op);
int rgbPixel = targetBitmap.getPixel(x, y);
// 转换字符
String r1 = Integer.toHexString(Color.red(rgbPixel));
String g1 = Integer.toHexString(Color.green(rgbPixel));
String b1 = Integer.toHexString(Color.blue(rgbPixel));
String colorStr = r1 + g1 + b1;
// Log.i("Value--------",colorStr);
TestReport.i(LOGTAG, "获取图片坐标点(" + "x-" + x + ",y-" + y + ")RGB值:" + colorStr);
return colorStr;
}
/**
* 获取当前界面中指定坐标点的RGB值,结果以16进制输出(例:ffffff)
* @param x 像素点的横坐标值
* @param y 像素点的纵坐标值
* @return 16进制的RGB值,以String类型返回。例:ffffff
* @throws AutoException
* @author ZeKyll
*/
public String getRGB(int x, int y) throws AutoException {
LogTimeGetter.updateTimeString();
String retRGB = "";
String imageFileName=screenshotTaker.takeScreenshot("_RGB");
File file = new File(imageFileName);
BitmapFactory.Options op = new BitmapFactory.Options();
op.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap targetBitmap = BitmapFactory.decodeFile(imageFileName, op);
int rgbPixel = targetBitmap.getPixel(x, y);
// 转换字符
String r1 = Integer.toHexString(Color.red(rgbPixel));
String g1 = Integer.toHexString(Color.green(rgbPixel));
String b1 = Integer.toHexString(Color.blue(rgbPixel));
String colorStr = r1 + g1 + b1;
// Log.i("Value--------",colorStr);
TestReport.i(LOGTAG, "获取当前屏幕坐标点(" + "x-" + x + ",y-" + y + ")RGB值:" + colorStr);
if (file.exists()){
file.delete();
}
return colorStr;
}
/***
* 截屏后直接保存在savepath下
*
* @param savename 截图文件名称
* @return 保存的截图文件的绝对路径
* @author ZeKyll
*/
public String takeScreenshot(String savename)
{
savename=df.format(new Date())+savename;
LogTimeGetter.updateTimeString();
screenshortDevice.takeScreenshot(new File(savepath+savename+".png"));
return savepath+savename+".png";
}
说明:这里获取的颜色值为一张图片中的一个坐标点,所以,需要给一张图片。第一个方法是自动截当前界面的图片,第二个是传递图片地址参数进入方法。