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
- One line of java code prints a heart
- Chinese Language Version of Linux Performance Monitoring Software netdata
- Interface Test Code Coverage (jacoco) Scheme Sharing
- Performance testing framework
- How to Enjoy Performance Testing on Linux Command Line Interface
- Graphic HTTP Brain Map
- How to Test Probabilistic Business Interface
- httpclient handles multi-user simultaneous online
- Automatically convert swagger documents into test code
- Five lines of code to build static blogs
- How httpclient handles 302 redirection
- A preliminary study on the testing framework of linear interface based on java
- Tcloud Cloud Measurement Platform
Selection of non-technical articles
- Why choose software testing as a career path?
- Ten Steps to Become a Great Java Developer
- Writing to everyone about programming thinking
- Obstacles to automated testing
- The Problems of Automated Testing
- Tested "Code Immortality" Brain Map
- Seven Steps to Become an Excellent Automated Testing Engineer
- Attitudes of Excellent Software Developers