zoukankan      html  css  js  c++  java
  • Appium自动化中截图的问题

    在用Appium做UI自动化过程中,大家会发现测试报告很重要,而在测试报告中截图很重要。

    因为很多公司都是用Jenkins作为持续集成工具,所以要让执行自动化测试的人看明白自动化在跑什么,哪里失败了,关键节点都需要截图。

    怎么做呢,目前项目中是这么实现的:

    1.实现截图功能类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    public static String screenShot(ShipperAndroidEmulator ae) {
        String dir = "screenshot"// TODO
        String time = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
        String screenShotPath = dir + File.separator + time + ".png";
     
        AndroidDriver augmentedDriver = null;
        augmentedDriver = ae.getAndroid();
     
     
        try {
            File sourceFile = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(sourceFile, new File(screenShotPath));
        catch (Exception e) {
            e.printStackTrace();
            return "Failed to screenshot";
        }
     
        return screenShotPath.replace("\""/");
    }

    2.用screenShot实现错误处理类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    private void handleFailure(String notice) {
        String png = LogTools.screenShot(this);
        String log = notice + " >> capture screenshot at " + png;
        logger.error(log);
        if (GlobalSettings.baseStorageUrl.lastIndexOf("/") == GlobalSettings.baseStorageUrl.length()) {
            GlobalSettings.baseStorageUrl = GlobalSettings.baseStorageUrl.substring(0, GlobalSettings.baseStorageUrl.length() - 1);
        }
        Reporter.log(log + "<br/><img src="" + GlobalSettings.baseStorageUrl + "/" + png + "" />");
        Assert.fail(log);
    }

    3.对所有appium界面操作类进行处理:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public void click(By by) {
     
        expectElementExistOrNot(true, by, timeout);
        try{
            clickTheClickable(by,System.currentTimeMillis(),2500);
            handleSuccess("Succeed to click " + by);
        }catch(Exception e){
            e.printStackTrace();
            handleFailure("Failed to click " + by);
        }
        logger.info("Clicked " + by);
    }

    4. 不能点击时候重试点击操作 

    private void clickTheClickable(By byElement, long startTime, int timeOut) throws Exception {
    try {
    findElementBy(byElement).click();
    } catch (Exception e) {
    if (System.currentTimeMillis() - startTime > timeOut) {
    logger.warn(byElement+ " is unclickable");
    throw new Exception(e);
    } else {
    Thread.sleep(500);
    logger.warn(byElement + " is unclickable, try again");
    clickTheClickable(byElement, startTime, timeOut);
    }
    }
  • 相关阅读:
    对象的访问定位——如何找到对象
    对象的结构
    对象在内存中的布局-对象的创建
    java的内存模型--jmm
    redis 持久化之rdb总结
    简单说springmvc的工作原理
    抽象类和接口的区别
    hashcode和equals的作用区别及联系
    DBC物品中打包物品参数设置
    关于GOM引擎启动时显示:windows socket error: 在其上下文中,该请求的地址无效。 (10049), on API 'bind'
  • 原文地址:https://www.cnblogs.com/jack1989/p/7687986.html
Copyright © 2011-2022 走看看