Appium自动化中截图的问题
在用Appium做UI自动化过程中,大家会发现测试报告很重要,而在测试报告中截图很重要。
因为很多公司都是用Jenkins作为持续集成工具,所以要让执行自动化测试的人看明白自动化在跑什么,哪里失败了,关键节点都需要截图。
怎么做呢,目前项目中是这么实现的:
1.实现截图功能类:
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实现错误处理类:
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界面操作类进行处理:
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); }
测试报告如下:
有截图