前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android UiAutomator如何根据颜色判断控件的状态

android UiAutomator如何根据颜色判断控件的状态

作者头像
FunTester
发布2019-09-17 15:30:36
2K0
发布2019-09-17 15:30:36
举报
文章被收录于专栏:FunTester

本人在用UiAutomator做测试的时候,经常会遇到一些控件因为不同的条件显示不同的颜色,在学习了UiAutomator图像处理之后,自己尝试写了一个方法来处理不同颜色控件的区分。分享代码供大家参考。

代码语言:javascript
复制
    //根据颜色判断状态
    public boolean isBlue(UiObject uiObject) throws UiObjectNotFoundException {
        screenShot("test");//截图
        String path = "/mnt/sdcard/123/test.png";
        Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象
        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);//获取坐标点像素颜色
                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;
    }

下面是在选择判定值的过程中快速获取某点颜色值的方法:

代码语言:javascript
复制
    public int getRedPixel(int x, int y) {
        screenShot("test");//截图
        String path = "/mnt/sdcard/123/test.png";
        Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象
        int color = bitmap.getPixel(x, y);//获取坐标点像素颜色
//        output(color);//输出颜色值
        int red = Color.red(color);
        return red;
    }
    public int getGreenPixel(int x, int y) {
        screenShot("test");//截图
        String path = "/mnt/sdcard/123/test.png";
        Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象
        int color = bitmap.getPixel(x, y);//获取坐标点像素颜色
//        output(color);//输出颜色值
        int green = Color.green(color);
        return green;
    }
    public int getBluePixel(int x, int y) {
        screenShot("test");//截图
        String path = "/mnt/sdcard/123/test.png";
        Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象
        int color = bitmap.getPixel(x, y);//获取坐标点像素颜色
//        output(color);//输出颜色值
        int blue = Color.blue(color);
        return blue;
    }
代码语言:javascript
复制
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;
    }

技术类文章精选

  1. java一行代码打印心形
  2. Linux性能监控软件netdata中文汉化版
  3. 接口测试代码覆盖率(jacoco)方案分享
  4. 性能测试框架
  5. 如何在Linux命令行界面愉快进行性能测试
  6. 图解HTTP脑图
  7. 如何测试概率型业务接口
  8. httpclient处理多用户同时在线
  9. 将swagger文档自动变成测试代码
  10. 五行代码构建静态博客
  11. httpclient如何处理302重定向
  12. 基于java的直线型接口测试框架初探
  13. Tcloud 云测平台--集大成者

非技术文章精选

  1. 为什么选择软件测试作为职业道路?
  2. 成为杰出Java开发人员的10个步骤
  3. 写给所有人的编程思维
  4. 自动化测试的障碍
  5. 自动化测试的问题所在
  6. 测试之《代码不朽》脑图
  7. 成为优秀自动化测试工程师的7个步骤
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-09-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 FunTester 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 技术类文章精选
  • 非技术文章精选
相关产品与服务
应用性能监控
应用性能监控(Application Performance Management,APM)是一款应用性能管理平台,基于实时多语言应用探针全量采集技术,为您提供分布式性能分析和故障自检能力。APM 协助您在复杂的业务系统里快速定位性能问题,降低 MTTR(平均故障恢复时间),实时了解并追踪应用性能,提升用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档