How android UiAutomator determines the state of a control based on its color

Keywords: Programming Java Linux

When I test with UiAutomator, I often encounter some controls that display different colors because of different conditions. After learning UiAutomator image processing, I try to write a method to deal with the distinction between different color controls. Share the code for your reference.

	//Judging state by color
	public boolean isBlue(UiObject uiObject) throws UiObjectNotFoundException {
		screenShot("test");//screenshot
		String path = "/mnt/sdcard/123/test.png";
		Bitmap bitmap = BitmapFactory.decodeFile(path);//Create and instantiate bitmap objects
		Rect rect = uiObject.getVisibleBounds();
		int x = rect.left;
		int xx = rect.right;
		int y = rect.top;
		int yy = rect.bottom;
		List<Integer> blueColor = new ArrayList<Integer>();
		for (int i = x; i < xx; i++) {
			for (int k = y;k < yy;k++) {
				int color = bitmap.getPixel(i, k);//Getting Pixel Color of Coordinate Points
				int red = Color.blue(color);
				blueColor.add(red);
			}
		}
		int sum = 0;
		for (int i = 0;i<blueColor.size();i++) {
			sum += blueColor.get(i);
		}
//		output(sum/blueColor.size());
		return sum/blueColor.size() > 200?true:false;
	}

The following is a quick way to get the color value of a certain point in the process of selecting the decision value:

	public int getRedPixel(int x, int y) {
		screenShot("test");//screenshot
		String path = "/mnt/sdcard/123/test.png";
		Bitmap bitmap = BitmapFactory.decodeFile(path);//Create and instantiate bitmap objects
		int color = bitmap.getPixel(x, y);//Getting Pixel Color of Coordinate Points
//		output(color);//output color value
		int red = Color.red(color);
		return red;
	}
	public int getGreenPixel(int x, int y) {
		screenShot("test");//screenshot
		String path = "/mnt/sdcard/123/test.png";
		Bitmap bitmap = BitmapFactory.decodeFile(path);//Create and instantiate bitmap objects
		int color = bitmap.getPixel(x, y);//Getting Pixel Color of Coordinate Points
//		output(color);//output color value
		int green = Color.green(color);
		return green;
	}
	public int getBluePixel(int x, int y) {
		screenShot("test");//screenshot
		String path = "/mnt/sdcard/123/test.png";
		Bitmap bitmap = BitmapFactory.decodeFile(path);//Create and instantiate bitmap objects
		int color = bitmap.getPixel(x, y);//Getting Pixel Color of Coordinate Points
//		output(color);//output color value
		int blue = Color.blue(color);
		return blue;
	}
public int[] getRGBcolorPixel(int x, int y) {
		screenShot("testDemo");
		String path = "/mnt/sdcard/123/testDemo.png";
		Bitmap bitmap = BitmapFactory.decodeFile(path);
		int color = bitmap.getPixel(x, y);
		int red = Color.red(color);
		int green = Color.green(color);
		int blue = Color.blue(color);
		int[] rgb = {red, green, blue};
		return rgb;
	}

Selection of Technical Articles

  1. One line of java code prints a heart
  2. Chinese Language Version of Linux Performance Monitoring Software netdata
  3. Interface Test Code Coverage (jacoco) Scheme Sharing
  4. Performance testing framework
  5. How to Enjoy Performance Testing on Linux Command Line Interface
  6. Graphic HTTP Brain Map
  7. How to Test Probabilistic Business Interface
  8. httpclient handles multi-user simultaneous online
  9. Automatically convert swagger documents into test code
  10. Five lines of code to build static blogs
  11. How httpclient handles 302 redirection
  12. A preliminary study on the testing framework of linear interface based on java
  13. Tcloud Cloud Measurement Platform

Selection of non-technical articles

  1. Why choose software testing as a career path?
  2. Ten Steps to Become a Great Java Developer
  3. Writing to everyone about programming thinking
  4. Obstacles to automated testing
  5. The Problems of Automated Testing
  6. Tested "Code Immortality" Brain Map
  7. Seven Steps to Become an Excellent Automated Testing Engineer
  8. Attitudes of Excellent Software Developers

Click on the Public Number Map

Posted by ident on Wed, 11 Sep 2019 20:35:49 -0700