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);
}
}
原文地址:https://www.cnblogs.com/jack1989/p/7687986.html